Repository navigation
Expand file tree
/
Copy pathsetupTests.js
More file actions
127 lines (105 loc) · 3.34 KB
/
Copy pathsetupTests.js
File metadata and controls
127 lines (105 loc) · 3.34 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable no-underscore-dangle */
import colours from 'colors';
import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'node:util';
import { ReadableStream } from 'node:stream/web';
import { MessageChannel, MessagePort } from 'node:worker_threads';
Object.assign(global, {
TextEncoder,
TextDecoder,
ReadableStream,
MessageChannel,
MessagePort,
});
Object.defineProperty(global, 'crypto', {
value: {
randomUUID: jest.fn(),
getRandomValues: jest.fn(),
},
});
global.setImmediate =
global.setImmediate || ((fn, ...args) => global.setTimeout(fn, 0, ...args));
/**
* Suppress JSDOM errors relating to navigation not implemented
* https://github.com/jsdom/jsdom/issues/2112 -> code snippet from https://github.com/jsdom/jsdom/issues/2112#issuecomment-673540137
* */
if (global.window?._virtualConsole) {
const listeners = window._virtualConsole.listeners('jsdomError');
const originalListener = listeners && listeners[0];
window._virtualConsole.removeAllListeners('jsdomError');
// Add a new listener to swallow JSDOM errors
window._virtualConsole.addListener('jsdomError', error => {
if (
error.message !== 'Not implemented: navigation (except hash changes)' &&
originalListener
) {
originalListener(error);
}
});
}
// Errors
const FAILED_PROP = 'Failed prop';
// Warnings
const PSEUDO_CLASS_FIRST_CHILD = 'The pseudo class ":first-child"';
const PSEUDO_CLASS_NTH_CHILD = 'The pseudo class ":nth-child"';
const UNMATCHED_GET = 'Unmatched GET to /undefined';
const REACT_UNMOUNTED = 'React state update on an unmounted component';
const TAG_HUNDEFINED = 'The tag <hundefined';
const SUPPRESSED_WARNINGS = [
PSEUDO_CLASS_FIRST_CHILD,
PSEUDO_CLASS_NTH_CHILD,
UNMATCHED_GET,
REACT_UNMOUNTED,
TAG_HUNDEFINED,
];
const SUPPRESSED_REGEX = new RegExp(SUPPRESSED_WARNINGS.join('|'));
const { warn } = console;
const getFormattedMessage = (message, rest) => {
let theMessage = message;
if (typeof message === 'object') {
if (message?.stack) {
theMessage = message?.stack.toString();
} else theMessage = JSON.stringify(message);
}
return theMessage.replace('%s', rest);
};
const didSuppressWarning = (message, ...rest) => {
const { expectedWarnings } = global.window || {};
if (SUPPRESSED_REGEX.test(message)) {
return true;
}
if (expectedWarnings && Array.isArray(expectedWarnings)) {
for (let i = 0; i < expectedWarnings.length; i += 1) {
const warningsRegex = new RegExp(
[FAILED_PROP, ...expectedWarnings[i]].join('*.*'),
);
const consoleFormattedMessage = getFormattedMessage(message, rest);
if (warningsRegex.test(consoleFormattedMessage)) {
expectedWarnings.splice(i, 1);
return true;
}
}
}
return false;
};
// eslint-disable-next-line no-console
console.error = (message, ...rest) => {
if (didSuppressWarning(message, ...rest)) return;
const formattedMessage = getFormattedMessage(message, rest);
throw new Error(
[
colours.red.bold(
`
${expect.getState().testPath}: ${expect.getState().currentTestName}
Please fix the following:
`,
),
colours.red(formattedMessage),
].join('\n'),
);
};
// eslint-disable-next-line no-console
console.warn = (message, ...rest) => {
if (didSuppressWarning(message, ...rest)) return;
warn(message, ...rest);
};