-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathimport.test.js
133 lines (108 loc) · 4.04 KB
/
import.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { transform } from '@babel/core';
import transformToCommonJsPlugin from '@babel/plugin-transform-modules-commonjs';
import { stripIndent } from 'common-tags';
import plugin from '../src';
describe('import and export statement', () => {
function testImport(source, output, transformerOpts) {
it('with an import statement', () => {
const code = `import something from "${source}";`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`import something from "${output}";`);
});
it('with an "export from" statement', () => {
const code = `export { something } from "${source}";`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`export { something } from "${output}";`);
});
it('with an export statement', () => {
const code = 'let something; export { something }';
const result = transform(code, transformerOpts);
expect(result.code).toBe('let something;\nexport { something };');
});
}
function testImportWithCommonJSTransform(source, output, transformerOpts) {
const transformerOptsWithCommonJs = {
babelrc: false,
plugins: [...transformerOpts.plugins, [transformToCommonJsPlugin, { noInterop: true }]],
};
it('with a transformed require statement', () => {
const code = `var something = require("${source}");`;
const result = transform(code, transformerOptsWithCommonJs);
expect(result.code).toContain(`require("${output}");`);
});
it('with a transformed import statement', () => {
const code = `import something from "${source}";`;
const result = transform(code, transformerOptsWithCommonJs);
expect(result.code).toContain(`require("${output}");`);
});
it('with a transformed "export from" statement', () => {
const code = `export { something } from "${source}";`;
const result = transform(code, transformerOptsWithCommonJs);
expect(result.code).toContain(`require("${output}");`);
});
it('with a transformed export statement', () => {
const code = 'let something; export { something };';
const result = transform(code, transformerOptsWithCommonJs);
expect(result.code).toBe(stripIndent`
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.something = void 0;
let something = exports.something = void 0;
`);
});
}
function testImports(source, output, transformerOpts) {
testImport(source, output, transformerOpts);
testImportWithCommonJSTransform(source, output, transformerOpts);
}
const transformerOpts = {
babelrc: false,
plugins: [
[
plugin,
{
root: './test/testproject/src',
alias: {
test: './test/testproject/test',
'@babel/core': '@babel/core/lib',
},
},
],
],
};
describe('should resolve the path based on the root config', () => {
testImports(
'components/Header/SubHeader',
'./test/testproject/src/components/Header/SubHeader',
transformerOpts
);
});
describe('should alias the path', () => {
testImports('test', './test/testproject/test', transformerOpts);
});
describe('should not change a relative path', () => {
testImports('./utils', './utils', transformerOpts);
});
describe('should handle an empty path', () => {
testImports('', '', transformerOpts);
});
describe('should only apply the alias once', () => {
// If this test breaks, consider selecting another package used by the plugin
testImports('@babel/core/transform', '@babel/core/lib/transform', transformerOpts);
});
describe('should ignore the call if a non-import statement is used', () => {
const code = stripIndent`
function test() {
return "components/Sidebar/Footer";
}
`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(stripIndent`
function test() {
return "components/Sidebar/Footer";
}
`);
});
});