Skip to content

Commit 1fe8286

Browse files
authoredMar 10, 2023
feat: uncaught errors to indicate env teardown (#2982)
1 parent f01c783 commit 1fe8286

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
lines changed
 

‎packages/vitest/src/node/error.ts

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
7272

7373
const testPath = (e as any).VITEST_TEST_PATH
7474
const testName = (e as any).VITEST_TEST_NAME
75+
const afterEnvTeardown = (e as any).VITEST_AFTER_ENV_TEARDOWN
7576
// testName has testPath inside
7677
if (testPath)
7778
ctx.logger.error(c.red(`This error originated in "${c.bold(testPath)}" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.`))
@@ -80,6 +81,11 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
8081
+ '\n- The error was thrown, while Vitest was running this test.'
8182
+ '\n- This was the last recorded test before the error was thrown, if error originated after test finished its execution.'))
8283
}
84+
if (afterEnvTeardown) {
85+
ctx.logger.error(c.red('This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:'
86+
+ '\n- cancel timeouts using clearTimeout and clearInterval'
87+
+ '\n- wait for promises to resolve using the await keyword'))
88+
}
8389

8490
if (typeof e.cause === 'object' && e.cause && 'name' in e.cause) {
8591
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
@@ -117,6 +123,7 @@ const skipErrorProperties = new Set([
117123
'expected',
118124
'VITEST_TEST_NAME',
119125
'VITEST_TEST_PATH',
126+
'VITEST_AFTER_ENV_TEARDOWN',
120127
...Object.getOwnPropertyNames(Error.prototype),
121128
...Object.getOwnPropertyNames(Object.prototype),
122129
])

‎packages/vitest/src/runtime/entry.ts

+2
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@ export async function run(files: string[], config: ResolvedConfig, environment:
100100

101101
await stopCoverageInsideWorker(config.coverage, executor)
102102
})
103+
104+
workerState.environmentTeardownRun = true
103105
}

‎packages/vitest/src/runtime/execute.ts

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export async function startViteNode(ctx: ContextRPC) {
5353
error.VITEST_TEST_NAME = worker.current?.name
5454
error.VITEST_TEST_PATH = relative(config.root, worker.filepath)
5555
}
56+
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun
57+
5658
rpc().onUnhandledError(error, type)
5759
}
5860

‎packages/vitest/src/types/worker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface WorkerGlobalState {
2323
rpc: BirpcReturn<RuntimeRPC>
2424
current?: Test
2525
filepath?: string
26+
environmentTeardownRun?: boolean
2627
moduleCache: ModuleCacheMap
2728
mockMap: MockMap
2829
}

‎test/core/test/dom.test.ts

-16
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,3 @@ it('doesn\'t throw, if listening for error', () => {
171171
dispatchEvent(new Event('custom'))
172172
expect(spy).toHaveBeenCalled()
173173
})
174-
175-
it('timers are not run after environment teardown', async () => {
176-
setInterval(() => {
177-
try {
178-
window.document.createElement('div')
179-
}
180-
catch (err) {
181-
if (/window is not defined/.test((err as any).message))
182-
throw new Error('setInterval was called after environment teardown')
183-
184-
throw err
185-
}
186-
}, 1)
187-
188-
expect(window.document).toBeDefined()
189-
})

0 commit comments

Comments
 (0)
Please sign in to comment.