diff --git a/lib/commands/instrument.js b/lib/commands/instrument.js index 5e56b44be..fc490890d 100644 --- a/lib/commands/instrument.js +++ b/lib/commands/instrument.js @@ -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') } @@ -63,6 +68,7 @@ exports.handler = function (argv) { require: argv.require, compact: argv.compact, preserveComments: argv.preserveComments, + esModules: argv.esModules, exitOnError: argv.exitOnError }) diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 9cc5cfe8b..37df86aa3 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -1,4 +1,4 @@ -/* global describe, it, beforeEach */ +/* global describe, it, beforeEach, afterEach */ const _ = require('lodash') const path = require('path') @@ -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() + }) + }) + }) }) })