Skip to content

Commit

Permalink
Fail tests on console.error() or console.warn()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Dec 1, 2022
1 parent da4243b commit 217696e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Libraries/Image/__tests__/ImageSourceUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('ImageSourceUtils', () => {
});

it('should warn when an unsupported scale is provided in srcSet', () => {
const mockWarn = jest.spyOn(console, 'warn');
const mockWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
let uri1 = 'uri1';
let scale1 = '300w';

Expand Down
4 changes: 3 additions & 1 deletion Libraries/Text/__tests__/Text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ describe('Text compat with web', () => {
`);
});

it('renders styles', () => {
// T138851366: prop-types are out of date causing a redbox
// eslint-disable-next-line jest/no-disabled-tests
it.skip('renders styles', () => {
const style = {
display: 'flex',
flex: 1,
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'<rootDir>/jest/assetFileTransformer.js',
'.*': './jest/private/preprocessor.js',
},
setupFiles: ['./jest/setup.js'],
setupFiles: ['./jest/local-setup.js'],
fakeTimers: {
enableGlobally: true,
legacyFakeTimers: true,
Expand Down
40 changes: 40 additions & 0 deletions jest/local-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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');

0 comments on commit 217696e

Please sign in to comment.