|
| 1 | +/* eslint-env jest */ |
| 2 | +import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies |
| 3 | +import plugin from '../src'; |
| 4 | + |
| 5 | +// According to https://github.com/tc39/proposal-dynamic-import |
| 6 | + |
| 7 | +describe('import()', () => { |
| 8 | + const transformerOpts = { |
| 9 | + babelrc: false, |
| 10 | + plugins: [ |
| 11 | + // We need to add the corresponding syntax plugin |
| 12 | + // in order to parse the `import()`-calls |
| 13 | + 'syntax-dynamic-import', |
| 14 | + [plugin, { |
| 15 | + root: [ |
| 16 | + './test/testproject/src', |
| 17 | + ], |
| 18 | + alias: { |
| 19 | + test: './test/testproject/test', |
| 20 | + }, |
| 21 | + }], |
| 22 | + ], |
| 23 | + }; |
| 24 | + |
| 25 | + it('should resolve the path based on the root config', () => { |
| 26 | + const code = 'import("app").then(() => {}).catch(() => {});'; |
| 27 | + const result = transform(code, transformerOpts); |
| 28 | + |
| 29 | + expect(result.code).toBe('import("./test/testproject/src/app").then(() => {}).catch(() => {});'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should alias the path', () => { |
| 33 | + const code = 'import("test/tools").then(() => {}).catch(() => {});'; |
| 34 | + const result = transform(code, transformerOpts); |
| 35 | + |
| 36 | + expect(result.code).toBe('import("./test/testproject/test/tools").then(() => {}).catch(() => {});'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should not change the path', () => { |
| 40 | + const code = 'import("./something").then(() => {}).catch(() => {});'; |
| 41 | + const result = transform(code, transformerOpts); |
| 42 | + |
| 43 | + expect(result.code).toBe('import("./something").then(() => {}).catch(() => {});'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle the first argument not being a string literal', () => { |
| 47 | + const code = 'import(path).then(() => {}).catch(() => {});'; |
| 48 | + const result = transform(code, transformerOpts); |
| 49 | + |
| 50 | + expect(result.code).toBe('import(path).then(() => {}).catch(() => {});'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle an empty path', () => { |
| 54 | + const code = 'import("").then(() => {}).catch(() => {});'; |
| 55 | + const result = transform(code, transformerOpts); |
| 56 | + |
| 57 | + expect(result.code).toBe('import("").then(() => {}).catch(() => {});'); |
| 58 | + }); |
| 59 | +}); |
0 commit comments