forked from tahowallet/extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupJest.ts
57 lines (51 loc) · 1.41 KB
/
setupJest.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
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as util from "util"
import Dexie from "dexie"
import { Tabs } from "webextension-polyfill"
// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
// ref: https://github.com/jsdom/jsdom/issues/2524
Object.defineProperty(window, "TextEncoder", {
writable: true,
value: util.TextEncoder,
})
Object.defineProperty(window, "TextDecoder", {
writable: true,
value: util.TextDecoder,
})
Object.defineProperty(window.navigator, "usb", {
writable: true,
value: {
getDevices: () => [],
addEventListener: () => {},
},
})
Object.defineProperty(browser, "alarms", {
writable: true,
value: {
create: () => {},
clear: () => {},
onAlarm: {
addListener: () => {},
removeListener: () => {},
},
},
})
// Mock top-level logger calls.
browser.extension.getBackgroundPage = jest.fn()
browser.tabs.getCurrent = jest.fn(() =>
// getCurrent can return undefined if there is no tab, and we act accordingly
// in the code.
Promise.resolve(undefined as unknown as Tabs.Tab)
)
// Prevent Dexie from caching indexedDB global so fake-indexeddb
// can reset properly.
Object.defineProperty(Dexie.dependencies, "indexedDB", {
get: () => indexedDB,
})
// Stub fetch calls
Object.defineProperty(window, "fetch", {
writable: true,
value: (url: string) => {
// eslint-disable-next-line no-console
console.warn("Uncaught fetch call to: \n", url)
},
})