-
Notifications
You must be signed in to change notification settings - Fork 47.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace console.error() with a throw in setTimeout() as last resort e…
…xception logging (#13310) * Add a regression test for #13188 * Replace console.error() with a throw in setTimeout() as last resort * Fix lint and comment * Fix tests to check we throw after all * Fix build tests
- Loading branch information
Showing
4 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
packages/react-dom/src/__tests__/ReactErrorLoggingRecovery-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
|
||
// This is a regression test for https://github.com/facebook/react/issues/13188. | ||
// It reproduces a combination of conditions that led to a problem. | ||
|
||
if (global.window) { | ||
throw new Error('This test must run in a Node environment.'); | ||
} | ||
|
||
// The issue only reproduced when React was loaded before JSDOM. | ||
const React = require('react'); | ||
const ReactDOM = require('react-dom'); | ||
|
||
// Unlike other tests, we want to enable error logging. | ||
// Note this is not a real Error prototype property, | ||
// it's only set in our Jest environment. | ||
// eslint-disable-next-line no-extend-native | ||
Error.prototype.suppressReactErrorLogging = false; | ||
|
||
// Initialize JSDOM separately. | ||
// We don't use our normal JSDOM setup because we want to load React first. | ||
const {JSDOM} = require('jsdom'); | ||
global.requestAnimationFrame = setTimeout; | ||
global.cancelAnimationFrame = clearTimeout; | ||
const jsdom = new JSDOM(`<div id="app-root"></div>`); | ||
global.window = jsdom.window; | ||
global.document = jsdom.window.document; | ||
global.navigator = jsdom.window.navigator; | ||
|
||
class Bad extends React.Component { | ||
componentDidUpdate() { | ||
throw new Error('no'); | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
describe('ReactErrorLoggingRecovery', () => { | ||
let originalConsoleError = console.error; | ||
|
||
beforeEach(() => { | ||
console.error = error => { | ||
throw new Error('Buggy console.error'); | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
console.error = originalConsoleError; | ||
}); | ||
|
||
it('should recover from errors in console.error', function() { | ||
const div = document.createElement('div'); | ||
let didCatch = false; | ||
try { | ||
ReactDOM.render(<Bad />, div); | ||
ReactDOM.render(<Bad />, div); | ||
} catch (e) { | ||
expect(e.message).toBe('no'); | ||
didCatch = true; | ||
} | ||
expect(didCatch).toBe(true); | ||
ReactDOM.render(<span>Hello</span>, div); | ||
expect(div.firstChild.textContent).toBe('Hello'); | ||
|
||
// Verify the console.error bug is surfaced | ||
expect(() => { | ||
jest.runAllTimers(); | ||
}).toThrow('Buggy console.error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters