Skip to content

Commit

Permalink
Merge pull request #8 from lijunle/test-refinement
Browse files Browse the repository at this point in the history
Test refinement.
  • Loading branch information
59naga committed Nov 14, 2015
2 parents 2e0dd6b + 103141e commit e27cbb9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 64 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"start": "mocha --bail --watch --require intelli-espower-loader",
"test": "istanbul cover _mocha --report text --report html --report lcov -- -R spec",
"test": "istanbul cover _mocha --report text --report html --report lcov -- -R spec --timeout 30000",
"posttest": "npm run lint",
"lint": "eslint lib test"
},
Expand All @@ -20,19 +20,16 @@
},
"devDependencies": {
"babel-core": "^6.1.2",
"babel-plugin-transform-es2015-modules-commonjs": "^6.1.3",
"babel-preset-es2015": "^6.1.2",
"eslint": "^1.9.0",
"eslint-config-standard": "^4.4.0",
"eslint-plugin-mocha": "^1.1.0",
"eslint-plugin-standard": "^1.3.1",
"espower-loader": "^1.0.0",
"fs-extra": "^0.26.2",
"intelli-espower-loader": "^1.0.0",
"istanbul": "^0.4.0",
"mocha": "^2.3.3",
"power-assert": "^1.1.0",
"rimraf": "^2.4.3"
"power-assert": "^1.1.0"
},
"keywords": [
"babel-plugin",
Expand Down
110 changes: 51 additions & 59 deletions test/index.js
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')
}))
})

0 comments on commit e27cbb9

Please sign in to comment.