From 596b1201ab736cbbe2b6ca41b55fb63f31d92d94 Mon Sep 17 00:00:00 2001 From: AndrewFinlay Date: Fri, 8 Mar 2019 07:28:04 +1100 Subject: [PATCH] feat: Enable `es-modules` option for nyc instrument command (#1006) BREAKING CHANGE: `nyc instrument` now enables the `--es-module` option by default. --- lib/commands/instrument.js | 6 +++++ test/nyc-integration.js | 49 +++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) 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() + }) + }) + }) }) })