forked from influxdata/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jestSetup.ts
71 lines (57 loc) · 1.57 KB
/
jestSetup.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {cleanup} from '@testing-library/react'
import 'intersection-observer'
import MutationObserver from 'mutation-observer'
import fetchMock from 'jest-fetch-mock'
import '@testing-library/jest-dom'
import {getMockedParse} from 'src/shared/utils/mocks/mockedParse'
import 'setimmediate'
// global vars
process.env.API_PREFIX = 'http://example.com/'
declare global {
interface Window {
flushAllPromises: () => Promise<any>
MutationObserver: MutationObserver
}
}
// Adds MutationObserver as a polyfill for testing
window.MutationObserver = MutationObserver
window.flushAllPromises = async () => {
return new Promise(resolve => setImmediate(resolve))
}
// mocks and stuff
fetchMock.enableMocks()
jest.mock('src/shared/utils/errors')
/*
N.B. when using react testing library render()
the mocked values in src/external/languages/flux/parser are ignored.
So, need to mock here as well
*/
jest.mock('src/languageSupport/languages/flux/parser', () => ({
parse: jest.fn(getMockedParse()),
format_from_js_file: jest.fn(),
}))
jest.mock('src/shared/workers/serviceWorker', () => ({
registerServiceWorker: jest.fn(),
}))
class Worker {
public url = ''
constructor(stringUrl) {
this.url = stringUrl
}
onmessage(_) {}
onerror(_) {}
postMessage(_) {}
terminate() {}
addEventListener(_, __, ___) {}
removeEventListener(_, __, ___) {}
dispatchEvent(_): boolean {
return false
}
}
window.Worker = Worker as any
// cleans up state between @testing-library/react tests
afterEach(() => {
window.localStorage.clear()
cleanup()
fetchMock.resetMocks()
})