2020-10-16 13:16:01 +00:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import colors from "colors/safe";
|
2019-09-06 08:58:23 +00:00
|
|
|
|
|
|
|
const shouldSilenceWarnings = (...messages) =>
|
2020-10-16 13:16:01 +00:00
|
|
|
[/Warning: componentWillReceiveProps has been renamed/].some((msgRegex) =>
|
|
|
|
messages.some((msg) => msgRegex.test(msg))
|
2019-09-06 08:58:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
global.window.app = {
|
2020-10-16 13:16:01 +00:00
|
|
|
mcApiUrl: "http://localhost:8080",
|
2019-09-06 08:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// setup file
|
|
|
|
const logOrThrow = (log, method, messages) => {
|
|
|
|
const warning = `console.${method} calls not allowed in tests`;
|
|
|
|
if (process.env.CI) {
|
|
|
|
if (shouldSilenceWarnings(messages)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
log(warning, "\n", ...messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
throw new Error(warning);
|
|
|
|
} else {
|
2020-10-16 13:16:01 +00:00
|
|
|
log(colors.bgYellow.black(" WARN "), warning, "\n", ...messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
const logMessage = console.log;
|
|
|
|
global.console.log = (...messages) => {
|
2020-10-16 13:16:01 +00:00
|
|
|
logOrThrow(logMessage, "log", messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
const logInfo = console.info;
|
|
|
|
global.console.info = (...messages) => {
|
2020-10-16 13:16:01 +00:00
|
|
|
logOrThrow(logInfo, "info", messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
const logWarning = console.warn;
|
|
|
|
global.console.warn = (...messages) => {
|
2020-10-16 13:16:01 +00:00
|
|
|
logOrThrow(logWarning, "warn", messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
const logError = console.error;
|
|
|
|
global.console.error = (...messages) => {
|
2020-10-16 13:16:01 +00:00
|
|
|
logOrThrow(logError, "error", messages);
|
2019-09-06 08:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Avoid unhandled promise rejections from going unnoticed
|
|
|
|
// https://github.com/facebook/jest/issues/3251#issuecomment-299183885
|
|
|
|
// In Node v7 unhandled promise rejections will terminate the process
|
|
|
|
if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
|
2020-10-16 13:16:01 +00:00
|
|
|
process.on("unhandledRejection", (reason) => {
|
|
|
|
logMessage("UNHANDLED REJECTION", reason);
|
2019-09-06 08:58:23 +00:00
|
|
|
|
|
|
|
// We create a file in case there is an unhandled rejection
|
|
|
|
// We later check for the existence of this file to fail CI
|
|
|
|
if (process.env.CI && !process.env.HAS_CREATED_UNHANDLED_REJECTION_FILE) {
|
|
|
|
const rootPath = process.cwd();
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(
|
|
|
|
rootPath,
|
2020-10-16 13:16:01 +00:00
|
|
|
"./fail-tests-because-there-was-an-unhandled-rejection.lock"
|
2019-09-06 08:58:23 +00:00
|
|
|
),
|
2020-10-16 13:16:01 +00:00
|
|
|
""
|
2019-09-06 08:58:23 +00:00
|
|
|
);
|
|
|
|
process.env.HAS_CREATED_UNHANDLED_REJECTION_FILE = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Avoid memory leak by adding too many listeners
|
|
|
|
process.env.LISTENING_TO_UNHANDLED_REJECTION = true;
|
|
|
|
}
|