Skip to content

Commit c9078a2

Browse files
authored
fix: create environment once per worker with isolate: false (#8915)
1 parent 9d2b4d5 commit c9078a2

File tree

31 files changed

+264
-211
lines changed

31 files changed

+264
-211
lines changed

packages/browser/src/client/client.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ export const ENTRY_URL: string = `${
2020
location.protocol === 'https:' ? 'wss:' : 'ws:'
2121
}//${HOST}/__vitest_browser_api__?type=${PAGE_TYPE}&rpcId=${RPC_ID}&sessionId=${getBrowserState().sessionId}&projectName=${getBrowserState().config.name || ''}&method=${METHOD}&token=${(window as any).VITEST_API_TOKEN || '0'}`
2222

23-
let setCancel = (_: CancelReason) => {}
24-
export const onCancel: Promise<CancelReason> = new Promise((resolve) => {
25-
setCancel = resolve
26-
})
23+
const onCancelCallbacks: ((reason: CancelReason) => void)[] = []
24+
25+
export function onCancel(callback: (reason: CancelReason) => void): void {
26+
onCancelCallbacks.push(callback)
27+
}
2728

2829
export interface VitestBrowserClient {
2930
rpc: BrowserRPC
@@ -74,7 +75,9 @@ function createClient() {
7475

7576
ctx.rpc = createBirpc<WebSocketBrowserHandlers, WebSocketBrowserEvents>(
7677
{
77-
onCancel: setCancel,
78+
async onCancel(reason) {
79+
await Promise.all(onCancelCallbacks.map(fn => fn(reason)))
80+
},
7881
async createTesters(options) {
7982
const orchestrator = await waitForOrchestrator()
8083
return orchestrator.createTesters(options)

packages/browser/src/client/tester/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export async function initiateRunner(
312312
})
313313
cachedRunner = runner
314314

315-
onCancel.then((reason) => {
315+
onCancel((reason) => {
316316
runner.cancel?.(reason)
317317
})
318318

packages/browser/src/client/tester/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const sessionId = getBrowserState().sessionId
88

99
const state: WorkerGlobalState = {
1010
ctx: {
11+
rpc: null as any,
1112
pool: 'browser',
1213
workerId: 1,
1314
config,

packages/browser/src/client/tester/tester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ async function prepareTestEnvironment(options: PrepareOptions) {
103103

104104
state.metaEnv = import.meta.env
105105
state.onCancel = onCancel
106+
state.ctx.rpc = rpc as any
106107
state.rpc = rpc as any
107108

108109
const interceptor = createModuleMockerInterceptor()
@@ -253,7 +254,6 @@ async function cleanup() {
253254
await rpc.wdioSwitchContext('parent')
254255
.catch(error => unhandledError(error, 'Cleanup Error'))
255256
}
256-
state.environmentTeardownRun = true
257257
await stopCoverageInsideWorker(config.coverage, moduleRunner, { isolate: config.browser.isolate }).catch((error) => {
258258
return unhandledError(error, 'Coverage Error')
259259
})

packages/ui/client/components/dashboard/ErrorEntry.vue

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,4 @@ defineProps<{
3030
</li>
3131
</ul>
3232
</div>
33-
<div v-if="error.VITEST_AFTER_ENV_TEARDOWN" text="sm" font-thin>
34-
This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:<br>
35-
<ul>
36-
<li>
37-
Cancel timeouts using clearTimeout and clearInterval.
38-
</li>
39-
<li>
40-
Wait for promises to resolve using the await keyword.
41-
</li>
42-
</ul>
43-
</div>
4433
</template>

packages/ui/client/shim.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ declare interface Window {
99

1010
declare interface Error {
1111
VITEST_TEST_NAME?: string
12-
VITEST_AFTER_ENV_TEARDOWN?: boolean
1312
VITEST_TEST_PATH?: string
1413
}

packages/vitest/src/integrations/env/jsdom.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function catchWindowErrors(window: DOMWindow) {
77
let userErrorListenerCount = 0
88
function throwUnhandlerError(e: ErrorEvent) {
99
if (userErrorListenerCount === 0 && e.error != null) {
10+
e.preventDefault()
1011
process.emit('uncaughtException', e.error)
1112
}
1213
}

packages/vitest/src/integrations/env/loader.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { BuiltinEnvironment, VitestEnvironment } from '../../node/types/config'
22
import type { Environment } from '../../types/environment'
3-
import type { ContextRPC, WorkerRPC } from '../../types/worker'
3+
import type { WorkerRPC } from '../../types/worker'
44
import { readFileSync } from 'node:fs'
55
import { isBuiltin } from 'node:module'
66
import { pathToFileURL } from 'node:url'
@@ -54,14 +54,13 @@ export async function createEnvironmentLoader(root: string, rpc: WorkerRPC): Pro
5454
}
5555

5656
export async function loadEnvironment(
57-
ctx: ContextRPC,
57+
name: string,
58+
root: string,
5859
rpc: WorkerRPC,
5960
): Promise<{ environment: Environment; loader?: ModuleRunner }> {
60-
const name = ctx.environment.name
6161
if (isBuiltinEnvironment(name)) {
6262
return { environment: environments[name] }
6363
}
64-
const root = ctx.config.root
6564
const loader = await createEnvironmentLoader(root, rpc)
6665
const packageId
6766
= name[0] === '.' || name[0] === '/'

packages/vitest/src/node/pool.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,12 @@ export function createPool(ctx: Vitest): ProcessPool {
147147

148148
taskGroup.push({
149149
context: {
150-
pool,
151-
config: project.serializedConfig,
152150
files: specs.map(spec => ({ filepath: spec.moduleId, testLocations: spec.testLines })),
153151
invalidates,
154-
environment,
155-
projectName: project.name,
156152
providedContext: project.getProvidedContext(),
157153
workerId: workerId++,
158154
},
155+
environment,
159156
project,
160157
env,
161158
execArgv,

packages/vitest/src/node/pools/pool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class Pool {
208208
distPath: this.options.distPath,
209209
project: task.project,
210210
method,
211-
environment: task.context.environment.name,
211+
environment: task.environment,
212212
env: task.env,
213213
execArgv: task.execArgv,
214214
}
@@ -280,7 +280,7 @@ function isEqualRunner(runner: PoolRunner, task: PoolTask) {
280280
return (
281281
runner.worker.name === task.worker
282282
&& runner.project === task.project
283-
&& runner.environment === task.context.environment.name
283+
&& runner.environment.name === task.environment.name
284284
&& (!runner.worker.canReuse || runner.worker.canReuse(task))
285285
)
286286
}

0 commit comments

Comments
 (0)