mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01:00
* Remove "Invariant Violation" from dev errors
When I made the change to compile `invariant` to throw expressions, I
left a small runtime to set the error's `name` property to "Invariant
Violation" to maintain the existing behavior.
I think we can remove it. The argument for keeping it is to preserve
continuity in error logs, but this only affects development errors,
anyway: production error messages are replaced with error codes.
* Pass prod error messages directly to constructor
Updates the `invariant` transform to pass an error message string
directly to the Error constructor, instead of mutating the
message property.
Turns this code:
```js
invariant(condition, 'A %s message that contains %s', adj, noun);
```
into this:
```js
if (!condition) {
throw Error(
__DEV__
? `A ${adj} message that contains ${noun}`
: formatProdErrorMessage(ERR_CODE, adj, noun)
);
}
```
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* 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
|
|
*/
|
|
'use strict';
|
|
|
|
let React;
|
|
let ReactDOM;
|
|
|
|
describe('ReactError', () => {
|
|
let globalErrorMock;
|
|
|
|
beforeEach(() => {
|
|
if (!__DEV__) {
|
|
// In production, our Jest environment overrides the global Error
|
|
// class in order to decode error messages automatically. However
|
|
// this is a single test where we actually *don't* want to decode
|
|
// them. So we assert that the OriginalError exists, and temporarily
|
|
// set the global Error object back to it.
|
|
globalErrorMock = global.Error;
|
|
global.Error = globalErrorMock.OriginalError;
|
|
expect(typeof global.Error).toBe('function');
|
|
}
|
|
jest.resetModules();
|
|
React = require('react');
|
|
ReactDOM = require('react-dom');
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (!__DEV__) {
|
|
global.Error = globalErrorMock;
|
|
}
|
|
});
|
|
|
|
if (__DEV__) {
|
|
it("empty test so Jest doesn't complain", () => {});
|
|
} else {
|
|
it('should error with minified error code', () => {
|
|
expect(() => ReactDOM.render('Hi', null)).toThrowError(
|
|
'Minified React error #200; visit ' +
|
|
'https://reactjs.org/docs/error-decoder.html?invariant=200' +
|
|
' for the full message or use the non-minified dev environment' +
|
|
' for full errors and additional helpful warnings.',
|
|
);
|
|
});
|
|
it('should serialize arguments', () => {
|
|
function Oops() {
|
|
return;
|
|
}
|
|
Oops.displayName = '#wtf';
|
|
const container = document.createElement('div');
|
|
expect(() => ReactDOM.render(<Oops />, container)).toThrowError(
|
|
'Minified React error #152; visit ' +
|
|
'https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=%23wtf' +
|
|
' for the full message or use the non-minified dev environment' +
|
|
' for full errors and additional helpful warnings.',
|
|
);
|
|
});
|
|
}
|
|
});
|