-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from tyler-johnson/babel-7
upgrade for babel 7
- Loading branch information
Showing
5 changed files
with
785 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,125 @@ | ||
import assert from 'assert' | ||
import { transform as babelTransform } from 'babel-core' | ||
import { transform as babelTransform7 } from '@babel/core' | ||
import { testPlugin, equal } from './helpers' | ||
import testCases from './spec' | ||
|
||
describe('babel-plugin-add-module-exports', () => { | ||
it('should not export default to `module.exports` by default.', () => | ||
testPlugin( | ||
testCases[0].code, | ||
{ | ||
presets: ['env'] | ||
}, | ||
module => { | ||
assert(module !== 'default-entry') | ||
assert(module.default === 'default-entry') | ||
} | ||
)) | ||
const babelVersions = { | ||
'babel@6': babelTransform, | ||
'babel@7': babelTransform7 | ||
} | ||
|
||
it('should not handle an pure esmodule', () => { | ||
const code = `export default 'default-entry';` | ||
const result = babelTransform(code, { | ||
presets: [['env', { modules: false }]], | ||
plugins: ['./src/index.js'] | ||
}) | ||
Object.keys(babelVersions).forEach(ver => { | ||
const transform = babelVersions[ver] | ||
const env = ver === 'babel@6' ? 'env' : '@babel/preset-env' | ||
|
||
// use code comparison instead of vm.runInNewContext(doesn't work `export` syntax) | ||
assert(code === result.code) | ||
}) | ||
describe('babel-plugin-add-module-exports ' + ver, () => { | ||
it('should not export default to `module.exports` by default.', () => | ||
testPlugin( | ||
transform, | ||
testCases[0].code, | ||
{ | ||
presets: [env] | ||
}, | ||
module => { | ||
assert(module !== 'default-entry') | ||
assert(module.default === 'default-entry') | ||
} | ||
)) | ||
|
||
it('should not handle an amd module', () => | ||
testPlugin( | ||
`export default 'default-entry';`, | ||
{ | ||
presets: [['env', { modules: 'amd' }]], | ||
it('should not handle an pure esmodule', () => { | ||
const code = `export default 'default-entry';` | ||
const result = transform(code, { | ||
presets: [[env, { modules: false }]], | ||
plugins: ['./src/index.js'] | ||
}, | ||
module => { | ||
assert(module.default === 'default-entry') | ||
}, | ||
true | ||
)) | ||
|
||
it('plugin should export to module.exports(#31)', () => { | ||
const plugin = require('../src') | ||
assert(typeof plugin === 'function') | ||
}) | ||
|
||
it('should handle duplicated plugin references (#1)', () => | ||
testPlugin( | ||
testCases[0].code, | ||
{ | ||
presets: ['env'], | ||
plugins: ['./src/index.js', './src/index.js', './src/index.js'] | ||
}, | ||
(module, code) => { | ||
assert(module === 'default-entry') | ||
|
||
// @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722 | ||
assert(module.default === undefined) | ||
|
||
assert( | ||
code === | ||
`"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;` | ||
) | ||
} | ||
)) | ||
}) | ||
|
||
it('should export with `babel-plugin-rewire` (#19)', () => | ||
testPlugin( | ||
"export default { stuff: 'things' }", | ||
{ | ||
presets: ['react', 'env'], | ||
plugins: ['./src/index.js', 'rewire'] | ||
}, | ||
module => { | ||
assert(module.stuff === 'things') | ||
} | ||
)) | ||
// use code comparison instead of vm.runInNewContext(doesn't work `export` syntax) | ||
assert(code === result.code) | ||
}) | ||
|
||
testCases.forEach(testCase => | ||
it(`should ${testCase.name}`, () => | ||
it('should not handle an amd module', () => | ||
testPlugin( | ||
testCase.code, | ||
transform, | ||
`export default 'default-entry';`, | ||
{ | ||
presets: [['env', testCase.env]], | ||
plugins: [ | ||
'transform-export-extensions', // use export-from syntax | ||
['./src/index.js', testCase.options] | ||
] | ||
presets: [[env, { modules: 'amd' }]], | ||
plugins: ['./src/index.js'] | ||
}, | ||
module => { | ||
// assert module root (module.exports) object | ||
equal(module, testCase.expected.module) | ||
|
||
// assert each common entry is exported without error | ||
Object.keys(testCase.expected.exports).forEach(key => | ||
equal(module[key], testCase.expected.exports[key]) | ||
) | ||
} | ||
assert(module.default === 'default-entry') | ||
}, | ||
true | ||
)) | ||
) | ||
|
||
it('plugin should export to module.exports(#31)', () => { | ||
const plugin = require('../src') | ||
assert(typeof plugin === 'function') | ||
}) | ||
|
||
if (ver === 'babel@6') { | ||
// babel 7 throws an error with duplicate plugins | ||
it('should handle duplicated plugin references (#1)', () => | ||
testPlugin( | ||
transform, | ||
testCases[0].code, | ||
{ | ||
presets: [env], | ||
plugins: ['./src/index.js', './src/index.js', './src/index.js'] | ||
}, | ||
(module, code) => { | ||
assert(module === 'default-entry') | ||
|
||
// @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722 | ||
assert(module.default === undefined) | ||
|
||
assert( | ||
code === | ||
`"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;` | ||
) | ||
} | ||
)) | ||
|
||
// rewire hasn't been updated for babel 7 | ||
// https://github.com/speedskater/babel-plugin-rewire/issues/209 | ||
it('should export with `babel-plugin-rewire` (#19)', () => | ||
testPlugin( | ||
transform, | ||
"export default { stuff: 'things' }", | ||
{ | ||
presets: ['react', env], | ||
plugins: ['./src/index.js', 'rewire'] | ||
}, | ||
module => { | ||
assert(module.stuff === 'things') | ||
} | ||
)) | ||
} | ||
|
||
testCases.forEach(testCase => | ||
it(`should ${testCase.name}`, () => | ||
testPlugin( | ||
transform, | ||
testCase.code, | ||
{ | ||
presets: [[env, testCase.env]], | ||
plugins: [ | ||
ver === 'babel@6' // use export-from syntax | ||
? 'transform-export-extensions' | ||
: '@babel/plugin-proposal-export-default-from', | ||
['./src/index.js', testCase.options] | ||
] | ||
}, | ||
module => { | ||
// assert module root (module.exports) object | ||
equal(module, testCase.expected.module) | ||
|
||
// assert each common entry is exported without error | ||
Object.keys(testCase.expected.exports).forEach(key => | ||
equal(module[key], testCase.expected.exports[key]) | ||
) | ||
} | ||
)) | ||
) | ||
}) | ||
}) |
Oops, something went wrong.