-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest-shared-setup.ts
98 lines (88 loc) · 2.69 KB
/
jest-shared-setup.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import failOnConsole from "jest-fail-on-console"
import "@testing-library/jest-dom"
import "cross-fetch/polyfill"
import { configure } from "@testing-library/react"
import { resetAllWhenMocks } from "jest-when"
import * as matchers from "jest-extended"
import { mockRouter } from "ol-test-utilities/mocks/nextNavigation"
expect.extend(matchers)
// env vars
process.env.NEXT_PUBLIC_MITOL_API_BASE_URL =
"http://api.test.learn.odl.local:8063"
process.env.NEXT_PUBLIC_ORIGIN = "http://test.learn.odl.local:8062"
process.env.NEXT_PUBLIC_EMBEDLY_KEY = "fake-embedly-key"
// Pulled from the docs - see https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
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(),
})),
})
Element.prototype.scrollIntoView = jest.fn()
/*
* This used to live in ol-ckeditor but we also need it now for NukaCarousel,
* so it's now here so it's available across the board.
*/
class FakeResizeObserver {
observe() {
/** pass */
}
unobserve() {
/** pass */
}
disconnect() {
/** pass */
}
}
const polyfillResizeObserver = () => {
if (window.ResizeObserver !== undefined) {
/**
* If this throws... I guess our test env supports it natively now.
* Welcome to the future!
*/
throw new Error("ResizeObserver is already defined.")
}
window.ResizeObserver = FakeResizeObserver
}
polyfillResizeObserver()
failOnConsole()
configure({
/**
* Adapted from https://github.com/testing-library/dom-testing-library/issues/773
* to make the error messages a bit more succinct.
*
* By default, testing-library prints much too much of the DOM.
*
* This does change the stacktrace a bit: The line causing the error is still
* there, but the line where the error is generated (below) is most visible.
*/
getElementError(message, _container) {
const error = new Error(message ?? "")
error.name = "TestingLibraryElementError"
return error
},
})
jest.mock("next/navigation", () => {
return {
...jest.requireActual("ol-test-utilities/mocks/nextNavigation")
.nextNavigationMocks,
}
})
afterEach(() => {
/**
* Clear all mock call counts between tests.
* This does NOT clear mock implementations.
* Mock implementations are always cleared between test files.
*/
jest.clearAllMocks()
resetAllWhenMocks()
mockRouter.setCurrentUrl("/")
window.history.replaceState({}, "", "/")
})