diff --git a/packages/vitest/src/runtime/vm.ts b/packages/vitest/src/runtime/vm.ts index d378ba68ad77..e5ee30e32ff0 100644 --- a/packages/vitest/src/runtime/vm.ts +++ b/packages/vitest/src/runtime/vm.ts @@ -72,6 +72,7 @@ export async function run(ctx: WorkerContext) { process.env.VITEST_WORKER_ID = String(ctx.workerId) process.env.VITEST_POOL_ID = String(poolId) + process.env.VITEST_VM_POOL = '1' if (!vm.getVmContext) throw new TypeError(`Environment ${ctx.environment.name} doesn't provide "getVmContext" method. It should return a context created by "vm.createContext" method.`) diff --git a/test/web-worker/test/init.test.ts b/test/web-worker/test/init.test.ts index 27f7434e5167..8b75947feb26 100644 --- a/test/web-worker/test/init.test.ts +++ b/test/web-worker/test/init.test.ts @@ -65,7 +65,9 @@ it('worker with invalid url throws an error', async () => { } }) expect(event).toBeInstanceOf(ErrorEvent) - expect(event.error).toBeInstanceOf(Error) + // Error is in different context when running in VM. This is consistent with jest. + if (!import.meta.env.VITEST_VM_POOL) + expect(event.error).toBeInstanceOf(Error) expect(event.error.message).toContain('Failed to load') }) diff --git a/test/web-worker/test/sharedWorker.spec.ts b/test/web-worker/test/sharedWorker.spec.ts index 792289f478b0..c245d4b5976c 100644 --- a/test/web-worker/test/sharedWorker.spec.ts +++ b/test/web-worker/test/sharedWorker.spec.ts @@ -49,7 +49,9 @@ it('throws an error on invalid path', async () => { } }) expect(event).toBeInstanceOf(ErrorEvent) - expect(event.error).toBeInstanceOf(Error) + // Error is in different context when running in VM. This is consistent with jest. + if (!import.meta.env.VITEST_VM_POOL) + expect(event.error).toBeInstanceOf(Error) expect(event.error.message).toContain('Failed to load') })