Visitar URL original
fix(vm): reset fake timers after each test file (fix #11679) (#11779) · vitest-dev/vitest@78c46ac · GitHub
Skip to content

Commit 78c46ac

Browse files
fix(vm): reset fake timers after each test file (fix #11679) (#11779)
1 parent 7a56fea commit 78c46ac

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export async function run(
146146
// mocks should not affect different files
147147
vi.restoreAllMocks()
148148
moduleRunner.mocker?.reset()
149+
// fake timers patch the worker's `process`, which outlives this file
150+
vi.useRealTimers()
149151
}
150152
})
151153
} finally {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
setupFiles: ['./leave-fake-timers-on.js'],
6+
},
7+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { vi } from 'vitest'
2+
3+
vi.useFakeTimers()

‎test/e2e/test/vm-threads.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,25 @@ test.for(['vmThreads', 'vmForks'] as const)(
766766
},
767767
)
768768

769+
// fake timers also replace `hrtime` on the worker's `process`, which is shared
770+
// by every file, so files that leave them on must not keep their context alive
771+
test.for(['vmThreads', 'vmForks'] as const)(
772+
'%s releases the vm contexts of files that leave fake timers on',
773+
async (pool) => {
774+
const { stderr, exitCode } = await runVitestCli(
775+
'run',
776+
'--root',
777+
'fixtures/leak-probe',
778+
'--config=./fake-timers.config.js',
779+
`--pool=${pool}`,
780+
'--environment=./leak-probe-env.ts',
781+
)
782+
783+
expect(stderr).toBe('')
784+
expect(exitCode).toBe(0)
785+
},
786+
)
787+
769788
// changing V8 flags at runtime invalidates the module code cache shared across
770789
// files on a worker: via the context's `node:v8` the cache is cleared up front,
771790
// via anything else (here the worker realm's binding) the rejection is caught

0 commit comments

Comments
 (0)