-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsetupTests.js
52 lines (45 loc) · 1.74 KB
/
setupTests.js
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
import '@testing-library/jest-dom/extend-expect'
import {configure, act} from '@testing-library/react'
import {queryCache} from 'react-query'
import * as auth from 'auth-provider'
import {server} from 'test/server'
import * as usersDB from 'test/data/users'
import * as listItemsDB from 'test/data/list-items'
import * as booksDB from 'test/data/books'
// we don't need the profiler in tests
jest.mock('components/profiler')
// set the location to the /list route as we auto-redirect users to that route
window.history.pushState({}, 'Home page', '/list')
// speeds up *ByRole queries a bit
// https://github.com/testing-library/dom-testing-library/issues/552
configure({defaultHidden: true})
// make debug output for TestingLibrary Errors larger
process.env.DEBUG_PRINT_LIMIT = 15000
// enable API mocking in test runs using the same request handlers
// as for the client-side mocking.
beforeAll(() => server.listen({onUnhandledRequest: 'error'}))
afterAll(() => server.close())
afterEach(() => server.resetHandlers())
// general cleanup
afterEach(async () => {
queryCache.clear()
await Promise.all([
auth.logout(),
usersDB.reset(),
booksDB.reset(),
listItemsDB.reset(),
])
})
// real times is a good default to start, individual tests can
// enable fake timers if they need, and if they have, then we should
// run all the pending timers (in `act` because this can trigger state updates)
// then we'll switch back to realTimers.
// it's important this comes last here because jest runs afterEach callbacks
// in reverse order and we want this to be run first so we get back to real timers
// before any other cleanup
afterEach(async () => {
if (setTimeout._isMockFunction) {
act(() => jest.runOnlyPendingTimers())
jest.useRealTimers()
}
})