-
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 #8 from lijunle/test-refinement
Test refinement.
- Loading branch information
Showing
2 changed files
with
53 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,63 @@ | ||
/* eslint eqeqeq: 1 */ | ||
|
||
// Dependencies | ||
var vm = require('vm') | ||
var babel = require('babel-core') | ||
var assert = require('power-assert') | ||
var fsExtra = require('fs-extra') | ||
var rimraf = require('rimraf') | ||
|
||
// Fixture | ||
var fixture = ` | ||
let foo= 'bar' | ||
export default 'baz' | ||
export {foo} | ||
` | ||
var fixturePath = 'node_modules/fixture' | ||
|
||
// Specs | ||
describe('babel-plugin-add-module-exports', function () { | ||
this.timeout(5000) // wait for load the `babel-preset-es2015` | ||
|
||
beforeEach(() => { | ||
try { | ||
rimraf.sync(fixturePath) | ||
delete require.cache[require.resolve('fixture')] | ||
} catch (e) { | ||
|
||
} | ||
}) | ||
|
||
it('Nope.', () => { | ||
var result = babel.transform(fixture, { | ||
presets: ['es2015'], | ||
plugins: ['transform-es2015-modules-commonjs'] | ||
}) | ||
fsExtra.outputFileSync(fixturePath + '/index.js', result.code) | ||
|
||
var object = require('fixture') | ||
assert(object.default === 'baz') | ||
assert(object.foo === 'bar') | ||
}) | ||
|
||
it('Add the `module.exports = Object.assign(exports.default,exports);` to EOF.', () => { | ||
var result = babel.transform(fixture, { | ||
function createSandbox () { | ||
const exports = {} | ||
const sandbox = { | ||
exports, | ||
module: { exports } | ||
} | ||
|
||
return sandbox | ||
} | ||
|
||
function testPlugin (options, fn) { | ||
const fixture = ` | ||
let foo= 'bar' | ||
export default 'baz' | ||
export {foo} | ||
` | ||
|
||
const result = babel.transform(fixture, options) | ||
const sandbox = createSandbox() | ||
|
||
vm.runInNewContext(result.code, sandbox) | ||
|
||
fn(sandbox.module.exports) | ||
} | ||
|
||
describe('babel-plugin-add-module-exports', () => { | ||
it('should not export default to `module.exports` by default.', () => | ||
testPlugin({ | ||
presets: ['es2015'] | ||
}, (module) => { | ||
assert(module.toString() !== 'baz') | ||
assert(module.default === 'baz') | ||
assert(module.foo === 'bar') | ||
})) | ||
|
||
it('should export default to `module.exports` with this plugin', () => | ||
testPlugin({ | ||
presets: ['es2015'], | ||
plugins: ['../lib/index.js'] | ||
}) | ||
fsExtra.outputFileSync(fixturePath + '/index.js', result.code) | ||
|
||
var object = require('fixture') | ||
assert(object.toString() === 'baz') // need to invoke toString explicitly | ||
assert(object.default === 'baz') | ||
assert(object.foo === 'bar') | ||
}) | ||
|
||
it('issue#1', () => { | ||
var result = babel.transform(fixture, { | ||
}, (module) => { | ||
assert(module.toString() === 'baz') // need to invoke toString explicitly | ||
assert(module.default === 'baz') | ||
assert(module.foo === 'bar') | ||
})) | ||
|
||
it('should handle duplicated plugin references (#1)', () => | ||
testPlugin({ | ||
presets: ['es2015'], | ||
plugins: [ | ||
'../lib/index.js', | ||
'../lib/index.js', | ||
'../lib/index.js' | ||
] | ||
}) | ||
fsExtra.outputFileSync(fixturePath + '/index.js', result.code) | ||
|
||
var object = require('fixture') | ||
assert(object.toString() === 'baz') // need to invoke toString explicitly | ||
assert(object.default === 'baz') | ||
assert(object.foo === 'bar') | ||
}) | ||
}, (module) => { | ||
assert(module.toString() === 'baz') // need to invoke toString explicitly | ||
assert(module.default === 'baz') | ||
assert(module.foo === 'bar') | ||
})) | ||
}) |