-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-tests.ts
51 lines (44 loc) · 1.2 KB
/
setup-tests.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
import '@testing-library/jest-dom'
import { TextDecoder, TextEncoder } from 'node:util'
import React from 'react'
import { server } from './mocks/server'
// polyfill text encoder/decoder
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
})
// Establish API mocking before all tests.
beforeAll(() =>
server.listen({
onUnhandledRequest: 'warn',
})
)
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers())
// Clean up after the tests are finished.
afterAll(() => server.close())
class Worker {
private url
private onmessage
constructor(stringUrl) {
this.url = stringUrl
this.onmessage = () => {}
}
postMessage(msg) {
this.onmessage(msg)
}
addEventListener() {}
removeEventListener() {}
}
// required due to SDK dependency
Object.defineProperty(window, 'Worker', { value: Worker })
Object.defineProperty(window, 'MockedWindowURL', {
value: {
createObjectURL: () => 'blob:mocked',
revokeObjectURL: () => {},
},
})
// required by any react component (almost all of them)
global.React = React
global.URL.createObjectURL = jest.fn()