From 4b41bbbf34a1519463e7a410af4c956f82ce48b2 Mon Sep 17 00:00:00 2001 From: "Keith J. Grant" Date: Thu, 1 Jul 2021 14:01:45 -0700 Subject: [PATCH] fail tests if anything logs an error --- awx/ui_next/src/setupTests.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/awx/ui_next/src/setupTests.js b/awx/ui_next/src/setupTests.js index eda52d9c7d..12d8dafcd9 100644 --- a/awx/ui_next/src/setupTests.js +++ b/awx/ui_next/src/setupTests.js @@ -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