Skip to content

Commit e4a2bc7

Browse files
authored
test: add regression test for duplicate nodes (#56)
1 parent 228483c commit e4a2bc7

File tree

4 files changed

+261
-15
lines changed

4 files changed

+261
-15
lines changed

__tests__/dev-expression-test.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let babel = require('@babel/core');
1313
let 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

2626
var 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
6161
if (__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
});

dev-expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module.exports = function(babel) {
7777
// code removal in a minifier will strip that out.
7878
var condition = node.arguments[0];
7979
var devInvariant = t.callExpression(
80-
node.callee,
80+
t.cloneNode(node.callee),
8181
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
8282
);
8383
devInvariant[SEEN_SYMBOL] = true;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"@babel/core": "^7.0.0"
3030
},
3131
"devDependencies": {
32+
"@babel/plugin-transform-modules-commonjs": "^7.15.4",
3233
"codecov": "^3.8.1",
33-
"jest": "^26.6.3"
34+
"jest": "^26.6.3",
35+
"prettier": "^2.4.1"
3436
}
3537
}

0 commit comments

Comments
 (0)