Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have jest check for window errors in a similar to console.error logs. Fixes #8260 #8278

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion scripts/jest/test-framework-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ jest.mock('ReactDOMFeatureFlags', () => {

var env = jasmine.getEnv();

var callCount = 0;
var oldError = console.error;
var oldWindowOnError = window.onerror;

var callCount = 0;
var windowCallCount = 0;
var newError = function() {
callCount++;
oldError.apply(this, arguments);
};
var newWindowOnError = function() {
windowCallCount++;
oldWindowOnError.apply(this, arguments);
}

console.error = newError;
window.onerror = newWindowOnError;

env.beforeEach(() => {
callCount = 0;
windowCallCount = 0;
jasmine.addMatchers({
toBeReset() {
return {
Expand All @@ -42,6 +51,15 @@ env.beforeEach(() => {
toNotHaveBeenCalled() {
return {
compare(actual) {
if (actual === newWindowOnError) {
return {
pass: windowCallCount === 0,
message:
'Expected test not to trigger a global window error. If the error is expected, mock ' +
'it out using spyOn(window, \'onerror\'); and test that the ' +
'error occurs.',
};
}
return {
pass: callCount === 0,
message:
Expand All @@ -57,4 +75,5 @@ env.beforeEach(() => {
env.afterEach(() => {
expect(console.error).toBeReset();
expect(console.error).toNotHaveBeenCalled();
expect(window.onerror).toNotHaveBeenCalled();
});