-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathsetup.ts
More file actions
82 lines (68 loc) · 2.45 KB
/
setup.ts
File metadata and controls
82 lines (68 loc) · 2.45 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Importing this here makes it work everywhere.
import '@testing-library/jest-dom';
// Importing this inside a setup.ts file makes the types available everywhere.
import 'jest-extended';
// This installs jest matchers as a side effect as well.
import fetchMock from 'fetch-mock';
import crypto from 'crypto';
import { NodeWorker, __shutdownWorkers } from './fixtures/node-worker';
import { autoMockResizeObserver } from './fixtures/mocks/resize-observer';
autoMockResizeObserver();
if (process.env.TZ !== 'UTC') {
throw new Error('Jest must be run from `yarn test`');
}
fetchMock.mockGlobal();
(global as any).fetchMock = fetchMock;
// Mock the effects of the file-loader which our Webpack config defines
// for files ending in .worker.js: The "default export" is the path to the file.
jest.mock('../utils/gz.worker.js', () => 'src/utils/gz.worker.js');
// Install a Worker class which is similar to the DOM Worker class.
(global as any).Worker = NodeWorker;
afterEach(function () {
// All node workers must be shut down at the end of the test run,
// otherwise Jest won't exit.
__shutdownWorkers();
});
afterEach(() => {
// The configuration to restore and reset all of the mocks in tests does not seem
// to be working correctly. Manually trigger this call here to ensure we
// don't get intermittents from one test's mocks affecting another test's mocks.
//
// See https://github.com/facebook/jest/issues/7654
jest.resetAllMocks();
jest.restoreAllMocks();
jest.clearAllTimers();
jest.useRealTimers();
// Do the same with fetch mocks
fetchMock.removeRoutes();
fetchMock.clearHistory();
});
expect.extend({
toHaveClass(received, className) {
if (!(received instanceof Element)) {
throw new Error(
`expected value ${received} to be an instance of Element.`
);
}
const pass = received.classList.contains(className);
if (pass) {
return {
message: () => `expected element to not have class ${className}`,
pass: true,
};
}
return {
message: () =>
`expected element to have class ${className}, current classes are ${received.className}`,
pass: false,
};
},
});
Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.webcrypto.subtle,
},
});