Files
react-native/jest/local-setup.js
T
Nick Gerleman 217696ee17 Fail tests on console.error() or console.warn()
Summary:
This makes it so that React Native unit tests will fail if code unexpectedly outputs a warning or error (which would show as a redbox error).

This logic split out from the normal `jest/setup.js` which is included by the jest-preset, to only effect our tests instead of existing RN Jest users.

Changelog:
[Internal][Changed] - Fail tests on `console.error()` or `console.warn()`

Reviewed By: huntie

Differential Revision: D41564032

fbshipit-source-id: 3cc7d3a8433fcb75f654669b9c350dea2da937a8
2022-12-01 06:59:04 -08:00

41 lines
985 B
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
// Global setup for tests local to the react-native repo. This setup is not
// included in the react-native Jest preset.
'use strict';
const consoleError = console.error;
const consoleWarn = console.warn;
jest.spyOn(console, 'debug').mockImplementation(() => {
// Blackhole console output
});
jest.spyOn(console, 'info').mockImplementation(() => {
// Blackhole console output
});
jest.spyOn(console, 'log').mockImplementation(() => {
// Blackhole console output
});
jest.spyOn(console, 'error').mockImplementation((...args) => {
consoleError(...args);
throw new Error('console.error() was called');
});
jest.spyOn(console, 'warn').mockImplementation((...args) => {
consoleWarn(...args);
throw new Error('console.warn() was called');
});
require('./setup');