From 91fe4853a525c118d532f72ebbb350b2a7c54ce0 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 15 Aug 2023 17:05:30 +0200 Subject: [PATCH] fix: don't crash when calling useFakeTimers in VM pool (#3958) --- packages/vitest/src/runtime/vm.ts | 3 +++ test/core/test/{timers.test.ts => fixtures/timers.suite.ts} | 2 +- test/core/test/timers-jsdom.test.ts | 3 +++ test/core/test/timers-node.test.ts | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-) rename test/core/test/{timers.test.ts => fixtures/timers.suite.ts} (99%) create mode 100644 test/core/test/timers-jsdom.test.ts create mode 100644 test/core/test/timers-node.test.ts diff --git a/packages/vitest/src/runtime/vm.ts b/packages/vitest/src/runtime/vm.ts index 6c016c1c5919..e9a12e015ba8 100644 --- a/packages/vitest/src/runtime/vm.ts +++ b/packages/vitest/src/runtime/vm.ts @@ -89,6 +89,9 @@ export async function run(ctx: WorkerContext) { context.process = process context.global = context context.console = createCustomConsole(state) + // TODO: don't hardcode setImmediate in fake timers defaults + context.setImmediate = setImmediate + context.clearImmediate = clearImmediate if (ctx.invalidates) { ctx.invalidates.forEach((fsPath) => { diff --git a/test/core/test/timers.test.ts b/test/core/test/fixtures/timers.suite.ts similarity index 99% rename from test/core/test/timers.test.ts rename to test/core/test/fixtures/timers.suite.ts index 499f69e28b7c..d20085a66c8c 100644 --- a/test/core/test/timers.test.ts +++ b/test/core/test/fixtures/timers.suite.ts @@ -11,7 +11,7 @@ */ import { afterEach, describe, expect, it, vi } from 'vitest' -import { FakeTimers } from '../../../packages/vitest/src/integrations/mock/timers' +import { FakeTimers } from '../../../../packages/vitest/src/integrations/mock/timers' class FakeDate extends Date {} diff --git a/test/core/test/timers-jsdom.test.ts b/test/core/test/timers-jsdom.test.ts new file mode 100644 index 000000000000..8a98fde89258 --- /dev/null +++ b/test/core/test/timers-jsdom.test.ts @@ -0,0 +1,3 @@ +// @vitest-environment jsdom + +import './fixtures/timers.suite' diff --git a/test/core/test/timers-node.test.ts b/test/core/test/timers-node.test.ts new file mode 100644 index 000000000000..7ea66c78e24f --- /dev/null +++ b/test/core/test/timers-node.test.ts @@ -0,0 +1,3 @@ +// @vitest-environment node + +import './fixtures/timers.suite'