From c18fb0a9a8eae5904298d87c62d9934243de8674 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Tue, 24 Sep 2019 13:36:49 -0400 Subject: [PATCH] fix: Honor eager setting (false by default) (#1179) This delays loading of istanbul-lib-instrument until it is needed. --- lib/instrumenters/istanbul.js | 9 ++++---- lib/instrumenters/noop.js | 4 +++- tap-snapshots/test-eager.js-TAP.test.js | 16 +++++++++++++ test/eager.js | 30 +++++++++++++++++++++++++ test/fixtures/eager.js | 5 +++++ 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tap-snapshots/test-eager.js-TAP.test.js create mode 100644 test/eager.js create mode 100644 test/fixtures/eager.js diff --git a/lib/instrumenters/istanbul.js b/lib/instrumenters/istanbul.js index a10c7d1ba..29462181b 100644 --- a/lib/instrumenters/istanbul.js +++ b/lib/instrumenters/istanbul.js @@ -1,10 +1,10 @@ 'use strict' -const { createInstrumenter } = require('istanbul-lib-instrument') -const convertSourceMap = require('convert-source-map') -const mergeSourceMap = require('merge-source-map') - function InstrumenterIstanbul (options) { + const { createInstrumenter } = require('istanbul-lib-instrument') + const convertSourceMap = require('convert-source-map') + const mergeSourceMap = require('merge-source-map') + const { plugins } = options const configPlugins = plugins ? { plugins } : {} @@ -28,6 +28,7 @@ function InstrumenterIstanbul (options) { // TODO: test source-map merging logic. if (options.produceSourceMap) { var lastSourceMap = instrumenter.lastSourceMap() + /* istanbul ignore else */ if (lastSourceMap) { if (sourceMap) { lastSourceMap = mergeSourceMap( diff --git a/lib/instrumenters/noop.js b/lib/instrumenters/noop.js index 7efe8bd80..b0c9f70f9 100644 --- a/lib/instrumenters/noop.js +++ b/lib/instrumenters/noop.js @@ -1,6 +1,8 @@ -const { readInitialCoverage } = require('istanbul-lib-instrument') +'use strict' function NOOP () { + const { readInitialCoverage } = require('istanbul-lib-instrument') + return { instrumentSync (code, filename) { const extracted = readInitialCoverage(code) diff --git a/tap-snapshots/test-eager.js-TAP.test.js b/tap-snapshots/test-eager.js-TAP.test.js new file mode 100644 index 000000000..7949315f1 --- /dev/null +++ b/tap-snapshots/test-eager.js-TAP.test.js @@ -0,0 +1,16 @@ +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +exports[`test/eager.js TAP eager disabled by default > stdout 1`] = ` +0 + +` + +exports[`test/eager.js TAP eager enabled > stdout 1`] = ` +1 + +` diff --git a/test/eager.js b/test/eager.js new file mode 100644 index 000000000..14ccddb7e --- /dev/null +++ b/test/eager.js @@ -0,0 +1,30 @@ +'use strict' + +const path = require('path') + +const t = require('tap') + +const { testSuccess } = require('./helpers') + +const cwd = path.resolve(__dirname, 'fixtures') + +t.test('eager disabled by default', t => testSuccess(t, { + args: [ + '--silent=true', + '--exclude=eager.js', + process.execPath, + './eager.js' + ], + cwd +})) + +t.test('eager enabled', t => testSuccess(t, { + args: [ + '--silent=true', + '--eager=true', + '--exclude=eager.js', + process.execPath, + './eager.js' + ], + cwd +})) diff --git a/test/fixtures/eager.js b/test/fixtures/eager.js new file mode 100644 index 000000000..473b9ff56 --- /dev/null +++ b/test/fixtures/eager.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node +'use strict' + +const lib = require.resolve('istanbul-lib-instrument') +console.log(Object.keys(require.cache).filter(s => s === lib).length)