mirror of
https://github.com/facebook/react.git
synced 2026-02-26 18:58:05 +00:00
A new module has been added (ReactFiberErrorLogger). This logs error information (call stack and component stack) to the console to make errors easier to debug. It also prompts users to use error boundaries if they are not already using them. In the future, perhaps this will be injectable, enabling users to provide their own handler for custom processing/logging. For the time being, this should help with issues like this / #2461.
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
// We want to globally mock this but jest doesn't let us do that by default
|
|
// for a file that already exists. So we have to explicitly mock it.
|
|
jest.mock('ReactDOM');
|
|
jest.mock('ReactDOMFeatureFlags', () => {
|
|
const flags = require.requireActual('ReactDOMFeatureFlags');
|
|
return Object.assign({}, flags, {
|
|
useFiber: flags.useFiber || !!process.env.REACT_DOM_JEST_USE_FIBER,
|
|
});
|
|
});
|
|
|
|
// Error logging varies between Fiber and Stack;
|
|
// Rather than fork dozens of tests, mock the error-logging file by default.
|
|
jest.mock('ReactFiberErrorLogger');
|
|
|
|
var env = jasmine.getEnv();
|
|
|
|
var callCount = 0;
|
|
var oldError = console.error;
|
|
var newError = function() {
|
|
callCount++;
|
|
oldError.apply(this, arguments);
|
|
};
|
|
|
|
console.error = newError;
|
|
|
|
env.beforeEach(() => {
|
|
callCount = 0;
|
|
jasmine.addMatchers({
|
|
toBeReset() {
|
|
return {
|
|
compare(actual) {
|
|
// TODO: Catch test cases that call spyOn() but don't inspect the mock
|
|
// properly.
|
|
if (actual !== newError && !jasmine.isSpy(actual)) {
|
|
return {
|
|
pass: false,
|
|
message: 'Test did not tear down console.error mock properly.',
|
|
};
|
|
}
|
|
return {pass: true};
|
|
},
|
|
};
|
|
},
|
|
toNotHaveBeenCalled() {
|
|
return {
|
|
compare(actual) {
|
|
return {
|
|
pass: callCount === 0,
|
|
message:
|
|
'Expected test not to warn. If the warning is expected, mock ' +
|
|
'it out using spyOn(console, \'error\'); and test that the ' +
|
|
'warning occurs.',
|
|
};
|
|
},
|
|
};
|
|
},
|
|
});
|
|
});
|
|
env.afterEach(() => {
|
|
expect(console.error).toBeReset();
|
|
expect(console.error).toNotHaveBeenCalled();
|
|
});
|
|
|
|
function wrapDevMatcher(obj, name) {
|
|
const original = obj[name];
|
|
obj[name] = function devMatcher() {
|
|
try {
|
|
original.apply(this, arguments);
|
|
} catch (e) {
|
|
global.__hadDevFailures = e.stack;
|
|
}
|
|
};
|
|
}
|
|
|
|
const expectDev = function expectDev(actual) {
|
|
const expectation = expect(actual);
|
|
if (global.__suppressDevFailures) {
|
|
Object.keys(expectation).forEach((name) => {
|
|
wrapDevMatcher(expectation, name);
|
|
wrapDevMatcher(expectation.not, name);
|
|
});
|
|
}
|
|
return expectation;
|
|
};
|
|
global.expectDev = expectDev;
|