fail tests if anything logs an error

This commit is contained in:
Keith J. Grant 2021-07-01 14:01:45 -07:00
parent adb6661015
commit 4b41bbbf34

View File

@ -14,13 +14,29 @@ require('@nteract/mockument');
// eslint-disable-next-line import/prefer-default-export
export const asyncFlush = () => new Promise(resolve => setImmediate(resolve));
// this ensures that debug messages don't get logged out to the console
// while tests are running i.e. websocket connect/disconnect
let consoleHasError = false;
const { error } = global.console;
global.console = {
...console,
// this ensures that debug messages don't get logged out to the console
// while tests are running i.e. websocket connect/disconnect
debug: jest.fn(),
// fail tests that log errors.
// adapted from https://github.com/facebook/jest/issues/6121#issuecomment-708330601
error: (...args) => {
consoleHasError = true;
error(...args);
},
};
afterEach(() => {
if (consoleHasError) {
consoleHasError = false;
throw new Error('Error logged to console');
}
});
// This global variable is part of our Content Security Policy framework
// and so this mock ensures that we don't encounter a reference error
// when running the tests