Skip to content

Commit

Permalink
feat: Enable es-modules option for nyc instrument command (#1006)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `nyc instrument` now enables the `--es-module` option by default.
  • Loading branch information
AndrewFinlay authored and coreyfarrell committed Mar 7, 2019
1 parent 3cb1861 commit 596b120
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/commands/instrument.js
Expand Up @@ -46,6 +46,11 @@ exports.builder = function (yargs) {
type: 'boolean',
description: 'should nyc exit when an instrumentation failure occurs?'
})
.option('es-modules', {
default: true,
type: 'boolean',
description: 'tell the instrumenter to treat files as ES Modules'
})
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
}

Expand All @@ -63,6 +68,7 @@ exports.handler = function (argv) {
require: argv.require,
compact: argv.compact,
preserveComments: argv.preserveComments,
esModules: argv.esModules,
exitOnError: argv.exitOnError
})

Expand Down
49 changes: 48 additions & 1 deletion test/nyc-integration.js
@@ -1,4 +1,4 @@
/* global describe, it, beforeEach */
/* global describe, it, beforeEach, afterEach */

const _ = require('lodash')
const path = require('path')
Expand Down Expand Up @@ -600,6 +600,53 @@ describe('the nyc cli', function () {
done()
})
})

describe('es-modules', function () {
afterEach(function () {
rimraf.sync(path.resolve(fixturesCLI, './output'))
})

it('instruments file with `package` keyword when es-modules is disabled', function (done) {
const args = [bin, 'instrument', '--no-es-modules', './not-strict.js', './output']

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('close', function (code) {
code.should.equal(0)
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output'))
subdirExists.should.equal(true)
const files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
files.should.include('not-strict.js')
done()
})
})

it('fails on file with `package` keyword when es-modules is enabled', function (done) {
const args = [bin, 'instrument', '--exit-on-error', './not-strict.js', './output']

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

let stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

proc.on('close', function (code) {
code.should.equal(1)
stdoutShouldEqual(stderr, `
Failed to instrument ./not-strict.js`)
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output'))
subdirExists.should.equal(false)
done()
})
})
})
})
})

Expand Down

0 comments on commit 596b120

Please sign in to comment.