diff --git a/packages/vitest/src/runtime/rpc.ts b/packages/vitest/src/runtime/rpc.ts index 19b10b3ab5db..7c4ea63ff253 100644 --- a/packages/vitest/src/runtime/rpc.ts +++ b/packages/vitest/src/runtime/rpc.ts @@ -42,10 +42,6 @@ function withSafeTimers(fn: () => void) { const promises = new Set>() export const rpcDone = async () => { - // Run possible setTimeouts, e.g. the onces used by ConsoleLogSpy - const { setTimeout } = getSafeTimers() - await new Promise(resolve => setTimeout(resolve)) - if (!promises.size) return const awaitable = Array.from(promises) diff --git a/packages/vitest/src/runtime/setup.node.ts b/packages/vitest/src/runtime/setup.node.ts index 835fffa558a2..6bb7f64ca248 100644 --- a/packages/vitest/src/runtime/setup.node.ts +++ b/packages/vitest/src/runtime/setup.node.ts @@ -183,6 +183,10 @@ export async function withEnv( await fn() } finally { + // Run possible setTimeouts, e.g. the onces used by ConsoleLogSpy + const { setTimeout } = getSafeTimers() + await new Promise(resolve => setTimeout(resolve)) + await env.teardown(globalThis) } } diff --git a/test/core/test/dom.test.ts b/test/core/test/dom.test.ts index 1f6a04010072..fe8575ba1817 100644 --- a/test/core/test/dom.test.ts +++ b/test/core/test/dom.test.ts @@ -171,3 +171,19 @@ it('doesn\'t throw, if listening for error', () => { dispatchEvent(new Event('custom')) expect(spy).toHaveBeenCalled() }) + +it('timers are not run after environment teardown', async () => { + setInterval(() => { + try { + window.document.createElement('div') + } + catch (err) { + if (/window is not defined/.test((err as any).message)) + throw new Error('setInterval was called after environment teardown') + + throw err + } + }, 1) + + expect(window.document).toBeDefined() +}) diff --git a/test/watch/test/stdout.test.ts b/test/watch/test/stdout.test.ts new file mode 100644 index 000000000000..528d81195f95 --- /dev/null +++ b/test/watch/test/stdout.test.ts @@ -0,0 +1,32 @@ +import { readFileSync, writeFileSync } from 'fs' +import { afterEach, expect, test } from 'vitest' + +import { startWatchMode, waitFor } from './utils' + +const testFile = 'fixtures/math.test.ts' +const testFileContent = readFileSync(testFile, 'utf-8') + +afterEach(() => { + writeFileSync(testFile, testFileContent, 'utf8') +}) + +test('console.log is visible on test re-run', async () => { + const vitest = await startWatchMode() + const testCase = ` +test('test with logging', () => { + console.log('First') + console.log('Second') + console.log('Third') + expect(true).toBe(true) +}) +` + + writeFileSync(testFile, `${testFileContent}${testCase}`, 'utf8') + + await waitFor(() => { + expect(vitest.getOutput()).toMatch('stdout | math.test.ts > test with logging') + expect(vitest.getOutput()).toMatch('First') + expect(vitest.getOutput()).toMatch('Second') + expect(vitest.getOutput()).toMatch('Third') + }) +})