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

fix: Add flag to allow instrumentation of non-strict scripts. #863

Merged
merged 1 commit into from Jul 5, 2018
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: 4 additions & 2 deletions index.js
Expand Up @@ -141,7 +141,8 @@ NYC.prototype._createInstrumenter = function () {
ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a),
produceSourceMap: this.config.produceSourceMap,
compact: this.config.compact,
preserveComments: this.config.preserveComments
preserveComments: this.config.preserveComments,
esModules: this.config.esModules
})
}

Expand Down Expand Up @@ -282,8 +283,9 @@ NYC.prototype._transformFactory = function (cacheDir) {
try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
} catch (e) {
debugLog('failed to instrument ' + filename + 'with error: ' + e.stack)
debugLog('failed to instrument ' + filename + ' with error: ' + e.stack)
if (this.config.exitOnError) {
console.error('Failed to instrument ' + filename)
process.exit(1)
} else {
instrumented = code
Expand Down
6 changes: 6 additions & 0 deletions lib/config-util.js
Expand Up @@ -115,6 +115,12 @@ Config.buildYargs = function (cwd) {
describe: 'cache babel transpilation results for improved performance',
global: false
})
.option('es-modules', {
default: false,
type: 'boolean',
describe: 'tell the instrumenter to treat files as ES Modules',
global: false
})
.option('extension', {
alias: 'e',
default: [],
Expand Down
2 changes: 1 addition & 1 deletion lib/instrumenters/istanbul.js
Expand Up @@ -13,7 +13,7 @@ function InstrumenterIstanbul (cwd, options) {
preserveComments: options.preserveComments,
produceSourceMap: options.produceSourceMap,
ignoreClassMethods: options.ignoreClassMethods,
esModules: true
esModules: options.esModules
})

return {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cli/not-strict.js
@@ -0,0 +1,5 @@
function package() {
return 1;
}

package();
70 changes: 70 additions & 0 deletions test/nyc-bin.js
Expand Up @@ -1030,6 +1030,76 @@ describe('the nyc cli', function () {
})
})

describe('es-modules', () => {
it('allows reserved word when es-modules is disabled', (done) => {
const args = [
bin,
'--cache', 'false',
process.execPath, './not-strict.js'
]

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

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdoutShouldEqual(stdout, `
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
not-strict.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|`)
done()
})
})

it('forbids reserved word when es-modules is not disabled', (done) => {
const args = [
bin,
'--cache', 'false',
'--es-modules', 'true',
'--exit-on-error', 'true',
process.execPath, './not-strict.js'
]

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

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

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

proc.on('close', function (code) {
code.should.equal(1)
stdoutShouldEqual(stderr, `
Failed to instrument ${path.join(fixturesCLI, 'not-strict.js')}`)
stdoutShouldEqual(stdout, `
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|`)
done()
})
})
})

describe('merge', () => {
it('combines multiple coverage reports', (done) => {
const args = [
Expand Down