-
Notifications
You must be signed in to change notification settings - Fork 25
/
vitest.setup.ts
64 lines (54 loc) · 1.8 KB
/
vitest.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
import '@testing-library/jest-dom/vitest'
import 'vitest-canvas-mock'
import { Globals } from '@react-spring/web'
import { cleanup, configure } from '@testing-library/react'
import mockRouter from 'next-router-mock'
import { afterEach, beforeAll, vi } from 'vitest'
// runs a setup before all test cases (e.g. setting up jsdom)
beforeAll(() => {
configure({ testIdAttribute: 'data-test-id' })
// https://www.react-spring.dev/docs/guides/testing#skipping-animations
Globals.assign({
skipAnimation: true,
})
})
// runs a cleanup after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup()
mockRouter.push('/')
})
// via https://github.com/vitest-dev/vitest/issues/821
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
// render all <Media>
// https://github.com/artsy/fresnel/blob/main/src/DynamicResponsive.tsx#L97C27-L97C37
matches: true,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
vi.mock('next/router', () => require('next-router-mock'))
vi.mock('next/dynamic', async () => {
const dynamicModule: any = await vi.importActual('next/dynamic')
return {
default: (loader: any) => {
const dynamicActualComp = dynamicModule.default
const RequiredComponent = dynamicActualComp(() =>
loader().then((mod: any) => mod.default || mod)
)
if (RequiredComponent?.render?.displayName) {
RequiredComponent.render.displayName = loader.toString()
}
RequiredComponent.preload
? RequiredComponent.preload()
: RequiredComponent.render.preload()
return RequiredComponent
},
}
})