GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: fix coverage of mocked CJS modules imported from ESM · nodejs/node@40a43ac

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules/esm
    • translators.js
  • test
    • fixtures/test-runner
      • coverage-with-mock
        • dependency.cjs
        • subject.mjs
      • output
        • coverage-with-mock-cjs.mjs
        • coverage-with-mock-cjs.snapshot
    • test-runner
      • test-output-coverage-with-mock-cjs.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ const kShouldNotSkipModuleHooks = { __proto__: null, shouldSkipModuleHooks: fals
108108
* @param {boolean} isMain - Whether the module is the entrypoint
109109
*/
110110
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);
112114

113115
const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
114116
// Cache the source map for the cjs module if present.
Collapse file

‎test/fixtures/test-runner/coverage-with-mock/dependency.cjs‎

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

‎test/fixtures/test-runner/coverage-with-mock/subject.mjs‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { dependency } from './dependency.cjs';
2+
3+
export function subject() {
4+
return dependency();
5+
}
Collapse file

‎test/fixtures/test-runner/output/coverage-with-mock-cjs.mjs‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

‎test/fixtures/test-runner/output/coverage-with-mock-cjs.snapshot‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

‎test/test-runner/test-output-coverage-with-mock-cjs.mjs‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
);

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.