mirror of
https://github.com/facebook/react.git
synced 2026-02-24 12:43:00 +00:00
Upgraded from Babel 6 to Babel 7. The only significant change seems to be the way `@babel/plugin-transform-classes` handles classes differently from `babel-plugin-transform-es2015-classes`. In regular mode, the former injects a `_createClass` function that increases the bundle size, and in the latter it removes the safeguard checks. However, this is okay because we don't all classes in new features, and we want to deprecate class usage in the future in the react repo. Co-authored-by: Luna Ruan <luna@fb.com> Co-authored-by: Abdul Rauf <abdulraufmujahid@gmail.com> Co-authored-by: Maksim Markelov <maks-markel@mail.ru>
88 lines
2.1 KiB
JavaScript
88 lines
2.1 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 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();
|
|
});
|
|
});
|