Skip to content

Commit

Permalink
test(jest): throw error if there is some unexpected console error (#1226
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vtsvetkov-splunk committed Jun 13, 2024
1 parent cd2907a commit 6902c1f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ui/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@ import { server } from './src/mocks/server';
*/
configure({ testIdAttribute: 'data-test' });

/**
* MSW mocking
*/
beforeAll(() =>
server.listen({
onUnhandledRequest: 'warn',
})
);
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

/**
* Failing tests if there is some console error during tests
*/
// eslint-disable-next-line import/no-mutable-exports
export let consoleError: jest.SpyInstance<void, Parameters<(typeof console)['error']>>;

beforeEach(() => {
// eslint-disable-next-line no-console
const originalConsoleError = console.error;
consoleError = jest.spyOn(console, 'error');
consoleError.mockImplementation((...args: Parameters<typeof console.error>) => {
originalConsoleError(...args);
throw new Error(
'Console error was called. Call consoleError.mockImplementation(() => {}) if this is expected.'
);
});
});

0 comments on commit 6902c1f

Please sign in to comment.