-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowserMock.js
More file actions
85 lines (72 loc) · 2.21 KB
/
browserMock.js
File metadata and controls
85 lines (72 loc) · 2.21 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from 'fs/promises'
import { performance } from 'perf_hooks'
import { fetch as fetchPolyfill } from 'whatwg-fetch'
import crypto from 'node:crypto'
Object.defineProperty(globalThis, 'crypto', { value: crypto })
Object.defineProperty(document, 'queryCommandSupported', {
value: jest.fn().mockImplementation(() => true)
})
window.process = undefined
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
})
Object.defineProperty(window, 'fetch', {
value: jest.fn(async (url, options) => {
if (url.startsWith('file:')) {
const content = await fs.readFile(new URL(url).pathname)
return {
json: async () => JSON.stringify(JSON.parse(content.toString())),
arrayBuffer: async () =>
content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength),
status: 200
}
} else {
return fetchPolyfill(url, options)
}
})
})
Object.defineProperty(URL, 'createObjectURL', {
value: jest.fn((blob) => {
return 'blob:not-working'
})
})
Object.defineProperty(window, 'Worker', {
value: class Worker {
constructor(stringUrl) {}
postMessage(msg) {}
terminate() {}
removeEventListener() {}
}
})
Object.defineProperty(window, 'ResizeObserver', {
value: class ResizeObserver {
constructor() {}
observe() {}
}
})
Object.defineProperty(window, 'Buffer', { value: undefined })
// Force override performance, for some reason the implementation is empty otherwise
let _performance = performance
// remove nodeTiming because otherwise VSCode refuse to detect the env as a browser env, and it also fails to detect a node env (no `process`) so it generates an error
performance.nodeTiming = undefined
Object.defineProperty(global, 'performance', {
get() {
return _performance
},
set(v) {
// ignore
}
})
global.CSS = { escape: (v) => v }
Element.prototype.scrollIntoView = jest.fn()
window.document.adoptedStyleSheets = []