diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index d18d7099280f62..6e7d946458d44e 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -120,8 +120,16 @@ function setupCoverageHooks(dir) { const { resolve } = require('path'); const coverageDirectory = resolve(cwd, dir); const { sourceMapCacheToObject } = require('internal/source_map'); - internalBinding('profiler').setCoverageDirectory(coverageDirectory); - internalBinding('profiler').setSourceMapCacheGetter(sourceMapCacheToObject); + + if (process.features.inspector) { + internalBinding('profiler').setCoverageDirectory(coverageDirectory); + internalBinding('profiler').setSourceMapCacheGetter(sourceMapCacheToObject); + } else { + process.emitWarning('The inspector is disabled, ' + + 'coverage could not be collected', + 'Warning'); + return ''; + } return coverageDirectory; } diff --git a/test/common/index.js b/test/common/index.js index 98a26872223cb9..00ebd283a0c3e9 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -651,6 +651,12 @@ function skipIfInspectorDisabled() { } } +function skipIfInspectorEnabled() { + if (process.features.inspector) { + skip('V8 inspector is enabled'); + } +} + function skipIfReportDisabled() { if (!process.config.variables.node_report) { skip('Diagnostic reporting is disabled'); @@ -783,6 +789,7 @@ module.exports = { skipIf32Bits, skipIfEslintMissing, skipIfInspectorDisabled, + skipIfInspectorEnabled, skipIfReportDisabled, skipIfWorker, diff --git a/test/parallel/test-coverage-with-inspector-disabled.js b/test/parallel/test-coverage-with-inspector-disabled.js new file mode 100644 index 00000000000000..0b0c2aea43fa60 --- /dev/null +++ b/test/parallel/test-coverage-with-inspector-disabled.js @@ -0,0 +1,24 @@ +'use strict'; + +const common = require('../common'); +common.skipIfInspectorEnabled(); + +const fixtures = require('../common/fixtures'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const env = { ...process.env, NODE_V8_COVERAGE: '/foo/bar' }; +const childPath = fixtures.path('v8-coverage/subprocess'); +const { status, stderr } = spawnSync( + process.execPath, + [childPath], + { env } +); + +const warningMessage = 'The inspector is disabled, ' + + 'coverage could not be collected'; + +assert.strictEqual(status, 0); +assert.strictEqual( + stderr.toString().includes(`Warning: ${warningMessage}`), + true +);