Skip to content

Commit bde75a3

Browse files
authoredMar 7, 2023
fix: prevent running test cases timers after environment teardown (#2971)
1 parent a1954cc commit bde75a3

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed
 

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

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ function withSafeTimers(fn: () => void) {
4242
const promises = new Set<Promise<unknown>>()
4343

4444
export const rpcDone = async () => {
45-
// Run possible setTimeouts, e.g. the onces used by ConsoleLogSpy
46-
const { setTimeout } = getSafeTimers()
47-
await new Promise(resolve => setTimeout(resolve))
48-
4945
if (!promises.size)
5046
return
5147
const awaitable = Array.from(promises)

‎packages/vitest/src/runtime/setup.node.ts

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ export async function withEnv(
183183
await fn()
184184
}
185185
finally {
186+
// Run possible setTimeouts, e.g. the onces used by ConsoleLogSpy
187+
const { setTimeout } = getSafeTimers()
188+
await new Promise(resolve => setTimeout(resolve))
189+
186190
await env.teardown(globalThis)
187191
}
188192
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,19 @@ 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+
})

‎test/watch/test/stdout.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { readFileSync, writeFileSync } from 'fs'
2+
import { afterEach, expect, test } from 'vitest'
3+
4+
import { startWatchMode, waitFor } from './utils'
5+
6+
const testFile = 'fixtures/math.test.ts'
7+
const testFileContent = readFileSync(testFile, 'utf-8')
8+
9+
afterEach(() => {
10+
writeFileSync(testFile, testFileContent, 'utf8')
11+
})
12+
13+
test('console.log is visible on test re-run', async () => {
14+
const vitest = await startWatchMode()
15+
const testCase = `
16+
test('test with logging', () => {
17+
console.log('First')
18+
console.log('Second')
19+
console.log('Third')
20+
expect(true).toBe(true)
21+
})
22+
`
23+
24+
writeFileSync(testFile, `${testFileContent}${testCase}`, 'utf8')
25+
26+
await waitFor(() => {
27+
expect(vitest.getOutput()).toMatch('stdout | math.test.ts > test with logging')
28+
expect(vitest.getOutput()).toMatch('First')
29+
expect(vitest.getOutput()).toMatch('Second')
30+
expect(vitest.getOutput()).toMatch('Third')
31+
})
32+
})

0 commit comments

Comments
 (0)
Please sign in to comment.