1212let babel = require ( '@babel/core' ) ;
1313let devExpression = require ( '../dev-expression' ) ;
1414
15- function transform ( input ) {
15+ function transform ( input , plugins ) {
1616 return babel . transform ( input , {
1717 plugins : [ devExpression ] ,
1818 } ) . code ;
@@ -25,7 +25,7 @@ function compare(input, output) {
2525
2626var oldEnv ;
2727
28- describe ( 'dev-expression' , function ( ) {
28+ describe ( 'dev-expression' , function ( ) {
2929 beforeEach ( ( ) => {
3030 oldEnv = process . env . NODE_ENV ;
3131 process . env . NODE_ENV = '' ;
@@ -38,56 +38,80 @@ describe('dev-expression', function() {
3838 describe ( '__DEV__' , ( ) => {
3939 it ( 'should replace __DEV__ in if' , ( ) => {
4040 compare (
41- `
41+ `
4242 if (__DEV__) {
4343 console.log('foo')
4444 }` ,
45- `if (process.env.NODE_ENV !== "production") {
45+ `if (process.env.NODE_ENV !== "production") {
4646 console.log('foo');
47- }`
47+ }` ,
4848 ) ;
4949 } ) ;
5050
5151 it ( 'should not replace locally-defined __DEV__' , ( ) => {
5252 compare (
53- `
53+ `
5454 const __DEV__ = false;
5555
5656 if (__DEV__) {
5757 console.log('foo')
5858 }` ,
59- `const __DEV__ = false;
59+ `const __DEV__ = false;
6060
6161if (__DEV__) {
6262 console.log('foo');
63- }`
63+ }` ,
6464 ) ;
6565 } ) ;
6666
6767 it ( 'should not replace object key' , ( ) => {
6868 compare (
69- `
69+ `
7070 const foo = {
7171 __DEV__: 'hey',
7272 }` ,
73- `const foo = {
73+ `const foo = {
7474 __DEV__: 'hey'
75- };`
75+ };` ,
7676 ) ;
7777 } ) ;
7878 } ) ;
7979
8080 it ( 'should replace warning calls' , ( ) => {
8181 compare (
8282 "warning(condition, 'a %s b', 'c');" ,
83- `process.env.NODE_ENV !== "production" ? warning(condition, 'a %s b', 'c') : void 0;`
83+ `process.env.NODE_ENV !== "production" ? warning(condition, 'a %s b', 'c') : void 0;` ,
8484 ) ;
8585 } ) ;
8686
8787 it ( 'should replace invariant calls' , ( ) => {
8888 compare (
8989 "invariant(condition, 'a %s b', 'c');" ,
90- `!condition ? process.env.NODE_ENV !== "production" ? invariant(false, 'a %s b', 'c') : invariant(false) : void 0;`
90+ `!condition ? process.env.NODE_ENV !== "production" ? invariant(false, 'a %s b', 'c') : invariant(false) : void 0;` ,
9191 ) ;
9292 } ) ;
93+
94+ it ( 'should replace invariant correctly when imported and compiled' , ( ) => {
95+ const code = babel . transform (
96+ `
97+ import invariant from 'invariant';
98+
99+ invariant(condition, 'a %s b', 'c');
100+ ` ,
101+ {
102+ plugins : [ '@babel/plugin-transform-modules-commonjs' , devExpression ] ,
103+ } ,
104+ ) . code ;
105+
106+ expect ( code ) . not . toContain ( 'invariant(' ) ;
107+ expect ( code ) . toMatchInlineSnapshot ( `
108+ "\\"use strict\\";
109+
110+ var _invariant = _interopRequireDefault(require(\\"invariant\\"));
111+
112+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
113+
114+ !condition ? process.env.NODE_ENV !== \\"production\\" ? (0, _invariant.default)(false, 'a %s b', 'c') : (0, _invariant.default)(false) : void 0;"
115+ ` ) ;
116+ } ) ;
93117} ) ;
0 commit comments