-
Notifications
You must be signed in to change notification settings - Fork 18
/
setupTests.ts
69 lines (60 loc) · 2.07 KB
/
setupTests.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
/* eslint-disable jest/no-standalone-expect */
import "@testing-library/jest-dom"
import axios from "axios"
import { cleanup } from "@testing-library/react"
import { LanguagePrefix, loadTranslations } from "../util/languageUtil"
const spies = {
delete: jest.spyOn(axios, "delete"),
get: jest.spyOn(axios, "get"),
post: jest.spyOn(axios, "post"),
put: jest.spyOn(axios, "put"),
create: jest.spyOn(axios, "create"),
}
// The current nanoid implementation is not compatible with jest. This is a temporary workaround until the issue is resolved
// https://github.com/ai/nanoid/issues/363
jest.mock("nanoid", () => {
return {
nanoid: () => {},
}
})
jest.mock("@unleash/proxy-client-react", () => {
return {
__esModule: true,
useFlag: () => true,
useFlagsStatus: () => ({ flagsError: false }),
FlagProvider: ({ children }) => children,
}
})
beforeEach(() => {
jest.resetAllMocks()
})
// fail test if api call has not been mocked up
afterEach(() => {
cleanup()
expect(spies.delete).not.toHaveBeenCalled()
expect(spies.get).not.toHaveBeenCalled()
expect(spies.post).not.toHaveBeenCalled()
expect(spies.put).not.toHaveBeenCalled()
})
// see: https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
window.matchMedia = 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(),
}))
void loadTranslations(LanguagePrefix.English)
// FIXME: Re-enable when this issue is deployed
// https://github.com/bloom-housing/bloom/issues/1378
// // fail on console warnings. This allows us to catch missing
// // translations warnings in our code. Snippet from
// // https://stackoverflow.com/a/50584643
// const error = console.error
// console.error = function (message: Error | string, ...args: unknown[]) {
// error.apply(console, args) // keep default behaviour
// throw message instanceof Error ? message : new Error(message)
// }