-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
throwing-console-patch.js
50 lines (38 loc) · 1.27 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
47
48
49
50
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) => {
const warning = `console.${method} calls not allowed in tests`;
if (!process.env.CI) {
log(colors.bgYellow.black(' WARN '), warning, '\n', ...messages);
return;
}
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);
};
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);
};