Files
react/scripts/error-codes/__tests__/transform-error-messages.js
Andrew Clark 875d05d553 Include full error messages in React Native build (#15363)
The React Native build does not minify error messages in production,
but it still needs to run the error messages transform to compile
`invariant` calls to `ReactError`. To do this, I added a `noMinify`
option to the Babel plugin. I also renamed it from
`minify-error-messages` to the more generic `transform-error-messages`.
2019-04-09 16:40:19 -07:00

98 lines
2.4 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.
*/
/* eslint-disable quotes */
'use strict';
let babel = require('babel-core');
let devExpressionWithCodes = require('../transform-error-messages');
function transform(input, options = {}) {
return babel.transform(input, {
plugins: [[devExpressionWithCodes, options]],
}).code;
}
let oldEnv;
describe('error transform', () => {
beforeEach(() => {
oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = '';
});
afterEach(() => {
process.env.NODE_ENV = oldEnv;
});
it('should replace simple invariant calls', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'Do not override existing functions.');
`)
).toMatchSnapshot();
});
it('should only add `ReactError` and `ReactErrorProd` once each', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'Do not override existing functions.');
invariant(condition, 'Do not override existing functions.');
`)
).toMatchSnapshot();
});
it('should support invariant calls with args', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'Expected %s target to be an array; got %s', foo, bar);
`)
).toMatchSnapshot();
});
it('should support invariant calls with a concatenated template string and args', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'Expected a component class, ' + 'got %s.' + '%s', Foo, Bar);
`)
).toMatchSnapshot();
});
it('should correctly transform invariants that are not in the error codes map', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'This is not a real error message.');
`)
).toMatchSnapshot();
});
it('should handle escaped characters', () => {
expect(
transform(`
import invariant from 'shared/invariant';
invariant(condition, 'What\\'s up?');
`)
).toMatchSnapshot();
});
it('should support noMinify option', () => {
expect(
transform(
`
import invariant from 'shared/invariant';
invariant(condition, 'Do not override existing functions.');
`,
{noMinify: true}
)
).toMatchSnapshot();
});
});