-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
throwing-console-patch.js
46 lines (36 loc) 路 1.36 KB
/
throwing-console-patch.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
// eslint-disable-next-line
const colors = require('colors/safe');
const shouldSilenceWarnings = (...messages) =>
[].some((msgRegex) => messages.some((msg) => msgRegex.test(msg)));
const shouldNotThrowWarnings = (...messages) =>
[].some((msgRegex) => messages.some((msg) => msgRegex.test(msg)));
const logOrThrow = (log, method, messages) => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const warning = `console.${method} calls not allowed in tests`;
if (process.env.CI) {
if (shouldSilenceWarnings(messages)) return;
log(warning, '\n', ...messages);
// NOTE: That some warnings should be logged allowing us to refactor graceully
// without having to introduce a breaking change.
if (shouldNotThrowWarnings(messages)) return;
throw new Error(...messages);
} else {
log(colors.bgYellow.black(' WARN '), warning, '\n', ...messages);
}
};
const logMessage = console.log;
global.console.log = (...messages) => {
logOrThrow(logMessage, 'log', messages);
};
const logInfo = console.info;
global.console.info = (...messages) => {
logOrThrow(logInfo, 'info', messages);
};
const logWarning = console.warn;
global.console.warn = (...messages) => {
logOrThrow(logWarning, 'warn', messages);
};
const logError = console.error;
global.console.error = (...messages) => {
logOrThrow(logError, 'error', messages);
};