From 24471600c4deea4f793dce80d316bf465d30003e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 3 Oct 2024 14:55:43 +0900 Subject: [PATCH 1/5] refactor: typing ExecuteOptions --- packages/vitest/src/public/execute.ts | 2 +- packages/web-worker/src/runner.ts | 3 ++- packages/web-worker/src/shared-worker.ts | 2 +- packages/web-worker/src/utils.ts | 3 ++- packages/web-worker/src/worker.ts | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/vitest/src/public/execute.ts b/packages/vitest/src/public/execute.ts index 05fef48145a4..627abddf40c6 100644 --- a/packages/vitest/src/public/execute.ts +++ b/packages/vitest/src/public/execute.ts @@ -1 +1 @@ -export { VitestExecutor } from '../runtime/execute' +export { VitestExecutor, ExecuteOptions } from '../runtime/execute' diff --git a/packages/web-worker/src/runner.ts b/packages/web-worker/src/runner.ts index 11d8fb628bb4..49d00ade6e47 100644 --- a/packages/web-worker/src/runner.ts +++ b/packages/web-worker/src/runner.ts @@ -1,7 +1,8 @@ +import type { ExecuteOptions } from 'vitest/execute' import { VitestExecutor } from 'vitest/execute' export class InlineWorkerRunner extends VitestExecutor { - constructor(options: any, private context: any) { + constructor(options: ExecuteOptions, private context: any) { super(options) } diff --git a/packages/web-worker/src/shared-worker.ts b/packages/web-worker/src/shared-worker.ts index 8da51396051f..2b427c068e39 100644 --- a/packages/web-worker/src/shared-worker.ts +++ b/packages/web-worker/src/shared-worker.ts @@ -135,7 +135,7 @@ export function createSharedWorkerConstructor(): typeof SharedWorker { return runner.executeFile(fsPath).then(() => { // worker should be new every time, invalidate its sub dependency - runnerOptions.moduleCache.invalidateSubDepTree([ + runnerOptions.moduleCache!.invalidateSubDepTree([ fsPath, runner.mocker.getMockPath(fsPath), ]) diff --git a/packages/web-worker/src/utils.ts b/packages/web-worker/src/utils.ts index 002105c83748..f05979708816 100644 --- a/packages/web-worker/src/utils.ts +++ b/packages/web-worker/src/utils.ts @@ -2,6 +2,7 @@ import { readFileSync as _readFileSync } from 'node:fs' import type { WorkerGlobalState } from 'vitest' import ponyfillStructuredClone from '@ungap/structured-clone' import createDebug from 'debug' +import type { ExecuteOptions } from 'vitest/execute' import type { CloneOption } from './types' // keep the reference in case it was mocked @@ -79,7 +80,7 @@ export function createMessageEvent( } } -export function getRunnerOptions(): any { +export function getRunnerOptions(): ExecuteOptions { const state = getWorkerState() const { config, rpc, moduleCache } = state diff --git a/packages/web-worker/src/worker.ts b/packages/web-worker/src/worker.ts index 0d13bfa57de1..edde17a3a9c3 100644 --- a/packages/web-worker/src/worker.ts +++ b/packages/web-worker/src/worker.ts @@ -132,7 +132,7 @@ export function createWorkerConstructor( return runner.executeFile(fsPath).then(() => { // worker should be new every time, invalidate its sub dependency - runnerOptions.moduleCache.invalidateSubDepTree([ + runnerOptions.moduleCache!.invalidateSubDepTree([ fsPath, runner.mocker.getMockPath(fsPath), ]) From 11064dec37327663e140fa834d9ae0adfcd6b948 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 3 Oct 2024 15:50:23 +0900 Subject: [PATCH 2/5] fix(web-worker): reuse main executor mocker --- packages/web-worker/src/runner.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/web-worker/src/runner.ts b/packages/web-worker/src/runner.ts index 49d00ade6e47..86abe720b923 100644 --- a/packages/web-worker/src/runner.ts +++ b/packages/web-worker/src/runner.ts @@ -3,7 +3,10 @@ import { VitestExecutor } from 'vitest/execute' export class InlineWorkerRunner extends VitestExecutor { constructor(options: ExecuteOptions, private context: any) { + // share the same mocker as main executor + const mocker = (globalThis as any).__vitest_mocker__ super(options) + this.mocker = (globalThis as any).__vitest_mocker__ = mocker } prepareContext(context: Record) { From 8baf1cbd2ae383e5edfdf240160fbd7b39d90ee6 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 3 Oct 2024 15:52:12 +0900 Subject: [PATCH 3/5] Revert "refactor: typing ExecuteOptions" This reverts commit 24471600c4deea4f793dce80d316bf465d30003e. --- packages/vitest/src/public/execute.ts | 2 +- packages/web-worker/src/runner.ts | 3 +-- packages/web-worker/src/shared-worker.ts | 2 +- packages/web-worker/src/utils.ts | 3 +-- packages/web-worker/src/worker.ts | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/vitest/src/public/execute.ts b/packages/vitest/src/public/execute.ts index 627abddf40c6..05fef48145a4 100644 --- a/packages/vitest/src/public/execute.ts +++ b/packages/vitest/src/public/execute.ts @@ -1 +1 @@ -export { VitestExecutor, ExecuteOptions } from '../runtime/execute' +export { VitestExecutor } from '../runtime/execute' diff --git a/packages/web-worker/src/runner.ts b/packages/web-worker/src/runner.ts index 86abe720b923..93ad8d2b2401 100644 --- a/packages/web-worker/src/runner.ts +++ b/packages/web-worker/src/runner.ts @@ -1,8 +1,7 @@ -import type { ExecuteOptions } from 'vitest/execute' import { VitestExecutor } from 'vitest/execute' export class InlineWorkerRunner extends VitestExecutor { - constructor(options: ExecuteOptions, private context: any) { + constructor(options: any, private context: any) { // share the same mocker as main executor const mocker = (globalThis as any).__vitest_mocker__ super(options) diff --git a/packages/web-worker/src/shared-worker.ts b/packages/web-worker/src/shared-worker.ts index 2b427c068e39..8da51396051f 100644 --- a/packages/web-worker/src/shared-worker.ts +++ b/packages/web-worker/src/shared-worker.ts @@ -135,7 +135,7 @@ export function createSharedWorkerConstructor(): typeof SharedWorker { return runner.executeFile(fsPath).then(() => { // worker should be new every time, invalidate its sub dependency - runnerOptions.moduleCache!.invalidateSubDepTree([ + runnerOptions.moduleCache.invalidateSubDepTree([ fsPath, runner.mocker.getMockPath(fsPath), ]) diff --git a/packages/web-worker/src/utils.ts b/packages/web-worker/src/utils.ts index f05979708816..002105c83748 100644 --- a/packages/web-worker/src/utils.ts +++ b/packages/web-worker/src/utils.ts @@ -2,7 +2,6 @@ import { readFileSync as _readFileSync } from 'node:fs' import type { WorkerGlobalState } from 'vitest' import ponyfillStructuredClone from '@ungap/structured-clone' import createDebug from 'debug' -import type { ExecuteOptions } from 'vitest/execute' import type { CloneOption } from './types' // keep the reference in case it was mocked @@ -80,7 +79,7 @@ export function createMessageEvent( } } -export function getRunnerOptions(): ExecuteOptions { +export function getRunnerOptions(): any { const state = getWorkerState() const { config, rpc, moduleCache } = state diff --git a/packages/web-worker/src/worker.ts b/packages/web-worker/src/worker.ts index edde17a3a9c3..0d13bfa57de1 100644 --- a/packages/web-worker/src/worker.ts +++ b/packages/web-worker/src/worker.ts @@ -132,7 +132,7 @@ export function createWorkerConstructor( return runner.executeFile(fsPath).then(() => { // worker should be new every time, invalidate its sub dependency - runnerOptions.moduleCache!.invalidateSubDepTree([ + runnerOptions.moduleCache.invalidateSubDepTree([ fsPath, runner.mocker.getMockPath(fsPath), ]) From 0bb408f8b29ea59359d1386434f333f21cc8cc14 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 3 Oct 2024 16:28:55 +0900 Subject: [PATCH 4/5] test: add test --- test/core/src/web-worker/mock/worker-dep.ts | 3 +++ test/core/src/web-worker/mock/worker.ts | 3 +++ test/core/test/web-worker-mock.test.ts | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 test/core/src/web-worker/mock/worker-dep.ts create mode 100644 test/core/src/web-worker/mock/worker.ts create mode 100644 test/core/test/web-worker-mock.test.ts diff --git a/test/core/src/web-worker/mock/worker-dep.ts b/test/core/src/web-worker/mock/worker-dep.ts new file mode 100644 index 000000000000..7bf5e2f34a39 --- /dev/null +++ b/test/core/src/web-worker/mock/worker-dep.ts @@ -0,0 +1,3 @@ +export function workerDep() { + return 'workerDep' +} diff --git a/test/core/src/web-worker/mock/worker.ts b/test/core/src/web-worker/mock/worker.ts new file mode 100644 index 000000000000..5239e5dfc23c --- /dev/null +++ b/test/core/src/web-worker/mock/worker.ts @@ -0,0 +1,3 @@ +import { workerDep } from './worker-dep' + +self.postMessage(workerDep()) diff --git a/test/core/test/web-worker-mock.test.ts b/test/core/test/web-worker-mock.test.ts new file mode 100644 index 000000000000..b9a461ee2487 --- /dev/null +++ b/test/core/test/web-worker-mock.test.ts @@ -0,0 +1,16 @@ +import '@vitest/web-worker' +import { expect, test, vi } from 'vitest' +import { workerDep } from '../src/web-worker/mock/worker-dep' + +vi.mock(import('../src/web-worker/mock/worker-dep'), () => ({ workerDep: () => 'mocked' })) + +test('mock', async () => { + expect(workerDep()).toMatchInlineSnapshot(`"mocked"`) + const worker = new Worker(new URL('../src/web-worker/mock/worker', import.meta.url)) + await new Promise((resolve) => { + worker.addEventListener('message', (e) => { + expect(e.data).toMatchInlineSnapshot(`"mocked"`) + resolve() + }) + }) +}) From 7b943a06bfbb26eb63eefa6d267b72bb21dc3907 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 3 Oct 2024 16:46:10 +0900 Subject: [PATCH 5/5] test: tweak --- test/core/test/web-worker-mock.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/core/test/web-worker-mock.test.ts b/test/core/test/web-worker-mock.test.ts index b9a461ee2487..46c77d48497b 100644 --- a/test/core/test/web-worker-mock.test.ts +++ b/test/core/test/web-worker-mock.test.ts @@ -7,10 +7,10 @@ vi.mock(import('../src/web-worker/mock/worker-dep'), () => ({ workerDep: () => ' test('mock', async () => { expect(workerDep()).toMatchInlineSnapshot(`"mocked"`) const worker = new Worker(new URL('../src/web-worker/mock/worker', import.meta.url)) - await new Promise((resolve) => { + const data = await new Promise((resolve) => { worker.addEventListener('message', (e) => { - expect(e.data).toMatchInlineSnapshot(`"mocked"`) - resolve() + resolve(e.data) }) }) + expect(data).toMatchInlineSnapshot(`"mocked"`) })