forked from get-convex/convex-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-vitest-environment.ts
44 lines (40 loc) · 1.27 KB
/
custom-vitest-environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { Environment } from "vitest";
import { builtinEnvironments, populateGlobal } from "vitest/environments";
import ws from "ws";
const nodeWebSocket = ws as unknown as typeof WebSocket;
const happy = builtinEnvironments["happy-dom"];
export default <Environment>{
name: "happy-dom-plus-ws",
transformMode: happy.transformMode,
// optional - only if you support "experimental-vm" pool
async setupVM(options) {
const { getVmContext: happyGetVmContext, teardown: happyTeardown } =
await happy.setupVM!(options);
return {
getVmContext() {
const context = happyGetVmContext();
return context;
},
teardown() {
return happyTeardown();
// called after all tests with this env have been run
},
};
},
async setup(global, options) {
const { teardown: happyTeardown } = await happy.setup(global, options);
//populateGlobal(global, original, {});
// Add the websocket here!
global.myNewGlobalVariable = 8;
global.WebSocket = nodeWebSocket;
// custom setup
return {
teardown(global) {
const ret = happyTeardown(global);
delete global.myNewGlobalVariable;
return ret;
// called after all tests with this env have been run
},
};
},
};