forked from silane/babel-plugin-replace-import-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
109 lines (92 loc) · 4.01 KB
/
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
/*
Copyright 2021 silane
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const babel = require('@babel/core');
const pluginCommonjs = require('@babel/plugin-transform-modules-commonjs');
const plugin = require('./src/index.js');
function transform(input, options) {
return babel.transform(input, {
plugins: [[plugin, options], pluginCommonjs],
}).code;
}
function assertDynamicImportArgument(code, expected){
code = code.replace('import(', 'import_(');
const import_ = jest.fn();
new Function('import_', code)(import_);
for(let [idx, e] of expected.entries())
expect(import_).toHaveBeenNthCalledWith(idx + 1, e);
}
describe('src/index.js', () => {
test('extension is correctly replaced without CommonJS translation', () => {
const input = 'import foo from "./foo.js";';
const options = { extMapping: { '.js': '.cjs', '.sj': '.sjc' } };
const code = babel.transform(input, { plugins: [[plugin, options]] }).code;
expect(code).toContain('import foo from "./foo.cjs"');
});
test('extension is correctly replaced', () => {
const input = 'import foo from "./foo.js";';
const options = { extMapping: { '.js': '.cjs', '.sj': '.sjc' } };
const code = transform(input, options);
expect(code).toContain('require("./foo.cjs")');
});
test('extension is correctly added', () => {
const input = 'import foo from "./foo";';
const options = { extMapping: { '': '.ext', '.foo': '.bar' } };
const code = transform(input, options);
expect(code).toContain('require("./foo.ext")');
});
test('extension is correctly removed', () => {
const input = 'import foo from "./foo.abc";';
const options = { extMapping: { '.abc': '', '.def': '.ghi' } };
const code = transform(input, options);
expect(code).toContain('require("./foo")');
});
test('extension is not changed when there is no mapping', () => {
const input = 'import foo from "./module.foo";';
const options = { extMapping: {
'.bar': '.foobar', '.fo': '.foba', '.o': '.fb',
}};
const code = transform(input, options);
expect(code).toContain('require("./module.foo")');
});
test('extension is not changed when not relative path', () => {
const input = 'import foo from "package.foo";';
const options = { extMapping: { '.foo': '.bar' } };
const code = transform(input, options);
expect(code).toContain('require("package.foo")');
});
test('extension in "named re-export" statement is correctly replaced',
() => {
const input = 'export { foo } from "./module.ext";';
const options = { extMapping: { '.ext': '.mjs' } };
const code = transform(input, options);
expect(code).toContain('require("./module.mjs")');
});
test('extension in "re-export all" statement is correctly replaced', () => {
const input = 'export * from "./module.ext";';
const options = { extMapping: { '.ext': '.mjs' } };
const code = transform(input, options);
expect(code).toContain('require("./module.mjs")');
});
test('extension in dynamic import is correctly replaced', () => {
const input = 'import("./module" + ".ext");';
const options = { extMapping: { '.ext': '.mjs' } };
let code = transform(input, options);
assertDynamicImportArgument(code, ['./module.mjs']);
});
test('multiple dot extension is correctly replaced', () => {
const input = 'export * from "./module.zzz.aaa";';
const options = { extMapping: { '.aaa': '.a', '.zzz.aaa': '.z' } };
const code = transform(input, options);
expect(code).toContain('require("./module.z")');
});
});