Skip to content

Commit 596b120

Browse files
AndrewFinlaycoreyfarrell
authored andcommitted
feat: Enable es-modules option for nyc instrument command (#1006)
BREAKING CHANGE: `nyc instrument` now enables the `--es-module` option by default.
1 parent 3cb1861 commit 596b120

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/commands/instrument.js

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ exports.builder = function (yargs) {
4646
type: 'boolean',
4747
description: 'should nyc exit when an instrumentation failure occurs?'
4848
})
49+
.option('es-modules', {
50+
default: true,
51+
type: 'boolean',
52+
description: 'tell the instrumenter to treat files as ES Modules'
53+
})
4954
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
5055
}
5156

@@ -63,6 +68,7 @@ exports.handler = function (argv) {
6368
require: argv.require,
6469
compact: argv.compact,
6570
preserveComments: argv.preserveComments,
71+
esModules: argv.esModules,
6672
exitOnError: argv.exitOnError
6773
})
6874

test/nyc-integration.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, it, beforeEach */
1+
/* global describe, it, beforeEach, afterEach */
22

33
const _ = require('lodash')
44
const path = require('path')
@@ -600,6 +600,53 @@ describe('the nyc cli', function () {
600600
done()
601601
})
602602
})
603+
604+
describe('es-modules', function () {
605+
afterEach(function () {
606+
rimraf.sync(path.resolve(fixturesCLI, './output'))
607+
})
608+
609+
it('instruments file with `package` keyword when es-modules is disabled', function (done) {
610+
const args = [bin, 'instrument', '--no-es-modules', './not-strict.js', './output']
611+
612+
const proc = spawn(process.execPath, args, {
613+
cwd: fixturesCLI,
614+
env: env
615+
})
616+
617+
proc.on('close', function (code) {
618+
code.should.equal(0)
619+
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output'))
620+
subdirExists.should.equal(true)
621+
const files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
622+
files.should.include('not-strict.js')
623+
done()
624+
})
625+
})
626+
627+
it('fails on file with `package` keyword when es-modules is enabled', function (done) {
628+
const args = [bin, 'instrument', '--exit-on-error', './not-strict.js', './output']
629+
630+
const proc = spawn(process.execPath, args, {
631+
cwd: fixturesCLI,
632+
env: env
633+
})
634+
635+
let stderr = ''
636+
proc.stderr.on('data', function (chunk) {
637+
stderr += chunk
638+
})
639+
640+
proc.on('close', function (code) {
641+
code.should.equal(1)
642+
stdoutShouldEqual(stderr, `
643+
Failed to instrument ./not-strict.js`)
644+
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output'))
645+
subdirExists.should.equal(false)
646+
done()
647+
})
648+
})
649+
})
603650
})
604651
})
605652

0 commit comments

Comments
 (0)