GeezerVison is showing github.com — module: fix coverage of mocked CJS modules imported from ESM · nodejs/node@40a43ac
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -108,7 +108,9 @@ const kShouldNotSkipModuleHooks = { __proto__: null, shouldSkipModuleHooks: fals | 108 | 108 | * @param {boolean} isMain - Whether the module is the entrypoint | 109 | 109 | */ | 110 | 110 | function loadCJSModule(module, source, url, filename, isMain) { | 111 | | - const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false); | | 111 | + // Use the full URL as the V8 resource name so that any search params | | 112 | + // (e.g. ?node-test-mock) are preserved in coverage reports. | | 113 | + const compileResult = compileFunctionForCJSLoader(source, url, false /* is_sea_main */, false); | 112 | 114 |
| 113 | 115 | const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult; | 114 | 116 | // Cache the source map for the cjs module if present. | |
Collapse file Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +throw new Error('This should never be called'); | | 2 | + | | 3 | +exports.dependency = function dependency() { | | 4 | + return 'foo'; | | 5 | +} | | 6 | + | | 7 | +exports.unused = function unused() { | | 8 | + return 'bar'; | | 9 | +} |
Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { dependency } from './dependency.cjs'; | | 2 | + | | 3 | +export function subject() { | | 4 | + return dependency(); | | 5 | +} |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { mock, test } from 'node:test'; | | 2 | + | | 3 | +const dependency = mock.fn(() => 'mock-return-value'); | | 4 | +mock.module('../coverage-with-mock/dependency.cjs', { namedExports: { dependency } }); | | 5 | + | | 6 | +const { subject } = await import('../coverage-with-mock/subject.mjs'); | | 7 | + | | 8 | +test('subject calls dependency', (t) => { | | 9 | + t.assert.strictEqual(subject(), 'mock-return-value'); | | 10 | + t.assert.strictEqual(dependency.mock.callCount(), 1); | | 11 | +}); |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +TAP version 13 | | 2 | +# Subtest: subject calls dependency | | 3 | +ok 1 - subject calls dependency | | 4 | + --- | | 5 | + duration_ms: * | | 6 | + type: 'test' | | 7 | + ... | | 8 | +1..1 | | 9 | +# tests 1 | | 10 | +# suites 0 | | 11 | +# pass 1 | | 12 | +# fail 0 | | 13 | +# cancelled 0 | | 14 | +# skipped 0 | | 15 | +# todo 0 | | 16 | +# duration_ms * | | 17 | +# start of coverage report | | 18 | +# ------------------------------------------------------------------------------- | | 19 | +# file | line % | branch % | funcs % | uncovered lines | | 20 | +# ------------------------------------------------------------------------------- | | 21 | +# test | | | | | | 22 | +# fixtures | | | | | | 23 | +# test-runner | | | | | | 24 | +# coverage-with-mock | | | | | | 25 | +# subject.mjs | 100.00 | 100.00 | 100.00 | | | 26 | +# output | | | | | | 27 | +# coverage-with-mock-cjs.mjs | 100.00 | 100.00 | 100.00 | | | 28 | +# ------------------------------------------------------------------------------- | | 29 | +# all files | 100.00 | 100.00 | 100.00 | | | 30 | +# ------------------------------------------------------------------------------- | | 31 | +# end of coverage report |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as common from '../common/index.mjs'; | | 2 | +import * as fixtures from '../common/fixtures.mjs'; | | 3 | +import { spawnAndAssert, defaultTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js'; | | 4 | + | | 5 | +if (!process.features.inspector) { | | 6 | + common.skip('inspector support required'); | | 7 | +} | | 8 | + | | 9 | +ensureCwdIsProjectRoot(); | | 10 | +await spawnAndAssert( | | 11 | + fixtures.path('test-runner/output/coverage-with-mock-cjs.mjs'), | | 12 | + defaultTransform, | | 13 | + { | | 14 | + flags: [ | | 15 | + '--disable-warning=ExperimentalWarning', | | 16 | + '--test-reporter=tap', | | 17 | + '--experimental-test-module-mocks', | | 18 | + '--experimental-test-coverage', | | 19 | + '--test-coverage-exclude=!test/**', | | 20 | + ], | | 21 | + }, | | 22 | +); |
|