|
1 | 1 | import assert from 'assert' |
2 | 2 | import { transform as babelTransform } from 'babel-core' |
| 3 | +import { transform as babelTransform7 } from '@babel/core' |
3 | 4 | import { testPlugin, equal } from './helpers' |
4 | 5 | import testCases from './spec' |
5 | 6 |
|
6 | | -describe('babel-plugin-add-module-exports', () => { |
7 | | - it('should not export default to `module.exports` by default.', () => |
8 | | - testPlugin( |
9 | | - testCases[0].code, |
10 | | - { |
11 | | - presets: ['env'] |
12 | | - }, |
13 | | - module => { |
14 | | - assert(module !== 'default-entry') |
15 | | - assert(module.default === 'default-entry') |
16 | | - } |
17 | | - )) |
| 7 | +const babelVersions = { |
| 8 | + 'babel@6': babelTransform, |
| 9 | + 'babel@7': babelTransform7 |
| 10 | +} |
18 | 11 |
|
19 | | - it('should not handle an pure esmodule', () => { |
20 | | - const code = `export default 'default-entry';` |
21 | | - const result = babelTransform(code, { |
22 | | - presets: [['env', { modules: false }]], |
23 | | - plugins: ['./src/index.js'] |
24 | | - }) |
| 12 | +Object.keys(babelVersions).forEach(ver => { |
| 13 | + const transform = babelVersions[ver] |
| 14 | + const env = ver === 'babel@6' ? 'env' : '@babel/preset-env' |
25 | 15 |
|
26 | | - // use code comparison instead of vm.runInNewContext(doesn't work `export` syntax) |
27 | | - assert(code === result.code) |
28 | | - }) |
| 16 | + describe('babel-plugin-add-module-exports ' + ver, () => { |
| 17 | + it('should not export default to `module.exports` by default.', () => |
| 18 | + testPlugin( |
| 19 | + transform, |
| 20 | + testCases[0].code, |
| 21 | + { |
| 22 | + presets: [env] |
| 23 | + }, |
| 24 | + module => { |
| 25 | + assert(module !== 'default-entry') |
| 26 | + assert(module.default === 'default-entry') |
| 27 | + } |
| 28 | + )) |
29 | 29 |
|
30 | | - it('should not handle an amd module', () => |
31 | | - testPlugin( |
32 | | - `export default 'default-entry';`, |
33 | | - { |
34 | | - presets: [['env', { modules: 'amd' }]], |
| 30 | + it('should not handle an pure esmodule', () => { |
| 31 | + const code = `export default 'default-entry';` |
| 32 | + const result = transform(code, { |
| 33 | + presets: [[env, { modules: false }]], |
35 | 34 | plugins: ['./src/index.js'] |
36 | | - }, |
37 | | - module => { |
38 | | - assert(module.default === 'default-entry') |
39 | | - }, |
40 | | - true |
41 | | - )) |
42 | | - |
43 | | - it('plugin should export to module.exports(#31)', () => { |
44 | | - const plugin = require('../src') |
45 | | - assert(typeof plugin === 'function') |
46 | | - }) |
47 | | - |
48 | | - it('should handle duplicated plugin references (#1)', () => |
49 | | - testPlugin( |
50 | | - testCases[0].code, |
51 | | - { |
52 | | - presets: ['env'], |
53 | | - plugins: ['./src/index.js', './src/index.js', './src/index.js'] |
54 | | - }, |
55 | | - (module, code) => { |
56 | | - assert(module === 'default-entry') |
57 | | - |
58 | | - // @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722 |
59 | | - assert(module.default === undefined) |
60 | | - |
61 | | - assert( |
62 | | - code === |
63 | | - `"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;` |
64 | | - ) |
65 | | - } |
66 | | - )) |
| 35 | + }) |
67 | 36 |
|
68 | | - it('should export with `babel-plugin-rewire` (#19)', () => |
69 | | - testPlugin( |
70 | | - "export default { stuff: 'things' }", |
71 | | - { |
72 | | - presets: ['react', 'env'], |
73 | | - plugins: ['./src/index.js', 'rewire'] |
74 | | - }, |
75 | | - module => { |
76 | | - assert(module.stuff === 'things') |
77 | | - } |
78 | | - )) |
| 37 | + // use code comparison instead of vm.runInNewContext(doesn't work `export` syntax) |
| 38 | + assert(code === result.code) |
| 39 | + }) |
79 | 40 |
|
80 | | - testCases.forEach(testCase => |
81 | | - it(`should ${testCase.name}`, () => |
| 41 | + it('should not handle an amd module', () => |
82 | 42 | testPlugin( |
83 | | - testCase.code, |
| 43 | + transform, |
| 44 | + `export default 'default-entry';`, |
84 | 45 | { |
85 | | - presets: [['env', testCase.env]], |
86 | | - plugins: [ |
87 | | - 'transform-export-extensions', // use export-from syntax |
88 | | - ['./src/index.js', testCase.options] |
89 | | - ] |
| 46 | + presets: [[env, { modules: 'amd' }]], |
| 47 | + plugins: ['./src/index.js'] |
90 | 48 | }, |
91 | 49 | module => { |
92 | | - // assert module root (module.exports) object |
93 | | - equal(module, testCase.expected.module) |
94 | | - |
95 | | - // assert each common entry is exported without error |
96 | | - Object.keys(testCase.expected.exports).forEach(key => |
97 | | - equal(module[key], testCase.expected.exports[key]) |
98 | | - ) |
99 | | - } |
| 50 | + assert(module.default === 'default-entry') |
| 51 | + }, |
| 52 | + true |
100 | 53 | )) |
101 | | - ) |
| 54 | + |
| 55 | + it('plugin should export to module.exports(#31)', () => { |
| 56 | + const plugin = require('../src') |
| 57 | + assert(typeof plugin === 'function') |
| 58 | + }) |
| 59 | + |
| 60 | + if (ver === 'babel@6') { |
| 61 | + // babel 7 throws an error with duplicate plugins |
| 62 | + it('should handle duplicated plugin references (#1)', () => |
| 63 | + testPlugin( |
| 64 | + transform, |
| 65 | + testCases[0].code, |
| 66 | + { |
| 67 | + presets: [env], |
| 68 | + plugins: ['./src/index.js', './src/index.js', './src/index.js'] |
| 69 | + }, |
| 70 | + (module, code) => { |
| 71 | + assert(module === 'default-entry') |
| 72 | + |
| 73 | + // @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722 |
| 74 | + assert(module.default === undefined) |
| 75 | + |
| 76 | + assert( |
| 77 | + code === |
| 78 | + `"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;` |
| 79 | + ) |
| 80 | + } |
| 81 | + )) |
| 82 | + |
| 83 | + // rewire hasn't been updated for babel 7 |
| 84 | + // https://github.com/speedskater/babel-plugin-rewire/issues/209 |
| 85 | + it('should export with `babel-plugin-rewire` (#19)', () => |
| 86 | + testPlugin( |
| 87 | + transform, |
| 88 | + "export default { stuff: 'things' }", |
| 89 | + { |
| 90 | + presets: ['react', env], |
| 91 | + plugins: ['./src/index.js', 'rewire'] |
| 92 | + }, |
| 93 | + module => { |
| 94 | + assert(module.stuff === 'things') |
| 95 | + } |
| 96 | + )) |
| 97 | + } |
| 98 | + |
| 99 | + testCases.forEach(testCase => |
| 100 | + it(`should ${testCase.name}`, () => |
| 101 | + testPlugin( |
| 102 | + transform, |
| 103 | + testCase.code, |
| 104 | + { |
| 105 | + presets: [[env, testCase.env]], |
| 106 | + plugins: [ |
| 107 | + ver === 'babel@6' // use export-from syntax |
| 108 | + ? 'transform-export-extensions' |
| 109 | + : '@babel/plugin-proposal-export-default-from', |
| 110 | + ['./src/index.js', testCase.options] |
| 111 | + ] |
| 112 | + }, |
| 113 | + module => { |
| 114 | + // assert module root (module.exports) object |
| 115 | + equal(module, testCase.expected.module) |
| 116 | + |
| 117 | + // assert each common entry is exported without error |
| 118 | + Object.keys(testCase.expected.exports).forEach(key => |
| 119 | + equal(module[key], testCase.expected.exports[key]) |
| 120 | + ) |
| 121 | + } |
| 122 | + )) |
| 123 | + ) |
| 124 | + }) |
102 | 125 | }) |
0 commit comments