Skip to content

Commit 5880076

Browse files
committed
Added suppress warning flag for spyOn(console)
1 parent 84048c7 commit 5880076

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

packages/react-dom/src/__tests__/utils/ReactDOMServerIntegrationTestUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ module.exports = function(initModules) {
4141
if (console.error.calls && console.error.calls.reset) {
4242
console.error.calls.reset();
4343
} else {
44-
spyOnDev(console, 'error');
44+
// TODO: Rewrite tests that use this helper to enumerate expeceted errors.
45+
// This will enable the helper to use the .toWarnDev() matcher instead of spying.
46+
spyOnDev(console, 'error', true);
4547
}
4648

4749
const result = await fn();

packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.internal.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
let React;
1414
let ReactNoop;
1515

16+
// NOTE: This test requires traditional spies for console.error verification.
17+
// The .toWarnDev() matcher treates unexpected warnings as errors.
18+
// Unfortunately, if an error boundary is active, ReactFiberScheduler swallows
19+
// any Errors that are thrown synchronously by .toWarnDev().
20+
// And Errors that are queued to be returned later by the matcher are preempted
21+
// by the errors intentionally thrown by the Components in this test.
22+
// This means the matcher could only produce false positives.
1623
describe('ReactIncrementalErrorLogging', () => {
1724
beforeEach(() => {
1825
jest.resetModules();
@@ -25,7 +32,7 @@ describe('ReactIncrementalErrorLogging', () => {
2532
}
2633

2734
it('should log errors that occur during the begin phase', () => {
28-
spyOnDevAndProd(console, 'error');
35+
spyOnDevAndProd(console, 'error', true);
2936

3037
class ErrorThrowingComponent extends React.Component {
3138
componentWillMount() {
@@ -70,7 +77,7 @@ describe('ReactIncrementalErrorLogging', () => {
7077
});
7178

7279
it('should log errors that occur during the commit phase', () => {
73-
spyOnDevAndProd(console, 'error');
80+
spyOnDevAndProd(console, 'error', true);
7481

7582
class ErrorThrowingComponent extends React.Component {
7683
componentDidMount() {
@@ -120,7 +127,7 @@ describe('ReactIncrementalErrorLogging', () => {
120127
try {
121128
React = require('react');
122129
ReactNoop = require('react-noop-renderer');
123-
spyOnDevAndProd(console, 'error');
130+
spyOnDevAndProd(console, 'error', true);
124131

125132
class ErrorThrowingComponent extends React.Component {
126133
render() {
@@ -167,7 +174,7 @@ describe('ReactIncrementalErrorLogging', () => {
167174
});
168175

169176
it('should relay info about error boundary and retry attempts if applicable', () => {
170-
spyOnDevAndProd(console, 'error');
177+
spyOnDevAndProd(console, 'error', true);
171178

172179
class ParentComponent extends React.Component {
173180
render() {

scripts/jest/setupTests.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
2020
const jasmineSpyOn = global.spyOn;
2121

2222
const noop = function() {};
23-
const spyOn = function(object, methodName) {
24-
if (object === console) {
23+
const spyOn = function(object, methodName, ignoreConsoleWarning) {
24+
if (
25+
object === console &&
26+
(methodName === 'warn' || methodName === 'error') &&
27+
!ignoreConsoleWarning
28+
) {
2529
throw new Error(
2630
'Do not spy on the console directly. ' +
2731
'Use toWarnDev() or toLowPriorityWarnDev() instead.'
2832
);
2933
}
3034

31-
jasmineSpyOn(object, methodName);
35+
return jasmineSpyOn(object, methodName);
3236
};
3337

3438
// Spying on console methods in production builds can mask errors.

0 commit comments

Comments
 (0)