forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.init.ts
97 lines (80 loc) · 2.93 KB
/
jest.init.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
import 'jest-enzyme/lib/index';
import '@testing-library/jest-dom';
// setup file
import { configure } from 'enzyme';
// @ts-ignore
import Adapter from 'enzyme-adapter-react-16';
// @ts-ignore
import regeneratorRuntime from 'regenerator-runtime';
// @ts-ignore
import registerRequireContextHook from 'babel-plugin-require-context-hook/register';
import EventEmitter from 'events';
registerRequireContextHook();
jest.mock('util-deprecate', () => (fn: any) => fn);
// mock console.info calls for cleaner test execution
global.console.info = jest.fn().mockImplementation(() => {});
global.console.debug = jest.fn().mockImplementation(() => {});
// mock local storage calls
const localStorageMock = {
getItem: jest.fn().mockName('getItem'),
setItem: jest.fn().mockName('setItem'),
clear: jest.fn().mockName('clear'),
};
// @ts-ignore
global.localStorage = localStorageMock;
// @ts-ignore
global.regeneratorRuntime = regeneratorRuntime;
configure({ adapter: new Adapter() });
/* Fail tests on PropType warnings
This allows us to throw an error in tests environments when there are prop-type warnings.
This should keep the tests free of warnings going forward.
*/
const ignoreList = [
(error: any) => error.message.includes('":nth-child" is potentially unsafe'),
(error: any) => error.message.includes('":first-child" is potentially unsafe'),
(error: any) => error.message.includes('Failed prop type') && error.stack.includes('storyshots'),
(error: any) =>
error.message.includes('react-async-component-lifecycle-hooks') &&
error.stack.includes('addons/knobs/src/components/__tests__/Options.js'),
];
const throwMessage = (type: any, message: any) => {
const error = new Error(`${type}${message}`);
if (!ignoreList.reduce((acc, item) => acc || item(error), false)) {
throw error;
}
};
const throwWarning = (message: any) => throwMessage('warn: ', message);
const throwError = (message: any) => throwMessage('error: ', message);
global.console.error = throwError;
global.console.warn = throwWarning;
// Mock for matchMedia since it's not yet implemented in JSDOM (https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom)
global.window.matchMedia = jest.fn().mockImplementation((query) => {
return {
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
};
});
class EventSourceMock {
static sources: EventSourceMock[] = [];
static reset() {
this.sources = [];
}
emitter: EventEmitter;
constructor() {
this.emitter = new EventEmitter();
EventSourceMock.sources.push(this);
}
addEventListener(event: string, cb: (data: any) => void) {
this.emitter.on(event, cb);
}
emit(event: string, data: any) {
this.emitter.emit(event, data);
}
}
global.window.EventSource = EventSourceMock as any;