Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add es-modules option to instrument command #1006

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/commands/instrument.js
Expand Up @@ -51,6 +51,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 @@ -68,6 +73,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