Skip to content

Commit 8db3a2a

Browse files
committed
Mock Worker globally and import vi in test mock helpers
happy-dom does not provide Worker, which breaks tests that import composables using web workers (filter, selectMany). Add a MockWorker to the global vitest setup mirroring the existing BroadcastChannel mock. The two test mock helpers (filter.ts, selectMany.ts) called `vi.mock` without importing `vi`. With `globals: false` in vitest.config.mts that throws ReferenceError. Import `vi` explicitly.
1 parent 8f56c20 commit 8db3a2a

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

client/src/components/Form/Elements/FormSelectMany/worker/__mocks__/selectMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { vi } from "vitest";
12
import { reactive, ref, watch } from "vue";
23

34
import type { SelectOption, useSelectMany as UseSelectMany } from "../selectMany";
45
import { main } from "../selectManyMain";
56

6-
// @ts-ignore - vi is a Vitest global
77
vi.mock("@/components/Form/Elements/FormSelectMany/worker/selectMany", () => ({
88
useSelectMany,
99
}));

client/src/composables/__mocks__/filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { toValue } from "@vueuse/core";
2+
import { vi } from "vitest";
23
import { computed, type Ref } from "vue";
34

45
import type { useFilterObjectArray as UseFilterObjectArray } from "@/composables/filter";
56

6-
// @ts-ignore - vi is a Vitest global
77
vi.mock("@/composables/filter", () => ({
88
useFilterObjectArray,
99
}));

client/tests/vitest/setup.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ Object.defineProperty(global, "BroadcastChannel", {
9999
value: MockBroadcastChannel,
100100
});
101101

102+
// Mock Worker so components using web workers (e.g. useFilterObjectArray)
103+
// don't throw "Worker is not defined" under happy-dom.
104+
class MockWorker extends EventTarget {
105+
onmessage: ((event: MessageEvent) => void) | null = null;
106+
onerror: ((event: ErrorEvent) => void) | null = null;
107+
onmessageerror: ((event: MessageEvent) => void) | null = null;
108+
109+
constructor(_url: string | URL, _options?: WorkerOptions) {
110+
super();
111+
}
112+
113+
postMessage(_message: unknown) {
114+
// No-op for tests
115+
}
116+
117+
terminate() {
118+
// No-op for tests
119+
}
120+
}
121+
122+
Object.defineProperty(global, "Worker", {
123+
writable: true,
124+
configurable: true,
125+
value: MockWorker,
126+
});
127+
102128
// Fail tests that log console errors or warnings
103129
// Replaces jest-fail-on-console functionality
104130
const failOnConsole = (await import("vitest-fail-on-console")).default;

0 commit comments

Comments
 (0)