Skip to content

Commit

Permalink
NODE - better ensureBabelRequire
Browse files Browse the repository at this point in the history
We now move all `babel` requires automatically to `nyc.require`
so the instrumentation for coverage will work
  • Loading branch information
stoffeastrom committed Nov 15, 2017
1 parent 7d8df0f commit 92ec72f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ class Runner {
return this;
}
ensureBabelRequire() {
// We need to remove `babel-register` for coverage since NYC needs to require it for instrumentation
if (this.argv.coverage && this.argv.nyc.babel && this.argv.require.includes('babel-register')) {
const ix = this.argv.require.indexOf('babel-register');
this.argv.require.splice(ix, 1);
// We need to move all `babel` requires to `nyc.require` else the instrumentation will not work
const containsBabelRequires = this.argv.require.filter(r => r.startsWith('babel'));
if (this.argv.coverage && this.argv.nyc.babel && containsBabelRequires.length) {
containsBabelRequires.forEach((r) => {
const ix = this.argv.require.indexOf(r);
const move = this.argv.require.splice(ix, 1)[0];
this.argv.nyc.require = [...new Set(this.argv.nyc.require.concat(move))];
});
}
return this;
}
Expand Down
5 changes: 3 additions & 2 deletions test/unit/node/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ describe('Node command', () => {
});

it('should ensureBabelRequire', () => {
const argv = { require: ['babel-register'], coverage: true, nyc: { babel: true } };
const argv = { require: ['babel-register', 'babel-helpers', 'foo'], coverage: true, nyc: { require: [], babel: true } };
const runner = new Runner(argv);
runner.ensureBabelRequire();
expect(runner.argv.require).to.eql([]);
expect(runner.argv.require).to.eql(['foo']);
expect(runner.argv.nyc.require).to.eql(['babel-register', 'babel-helpers']);
});

it('should require', () => {
Expand Down

0 comments on commit 92ec72f

Please sign in to comment.