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 — test_runner: support module detection in module mocks · nodejs/node@3000e5d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • internal/test_runner/mock
      • mock.js
    • test
      • mock_loader.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/test_runner/mock/mock.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ class MockTracker {
513513
mockSpecifier, caller, null,
514514
);
515515
debug('module mock, url = "%s", format = "%s", caller = "%s"', url, format, caller);
516-
validateOneOf(format, 'format', kSupportedFormats);
516+
if (format) { // Format is not yet known for ambiguous files when detection is enabled.
517+
validateOneOf(format, 'format', kSupportedFormats);
518+
}
517519
const baseURL = URL.parse(url);
518520

519521
if (!baseURL) {
Collapse file

‎lib/test/mock_loader.js‎

Copy file name to clipboardExpand all lines: lib/test/mock_loader.js
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,34 @@ async function load(url, context, nextLoad) {
143143
const baseURL = parsedURL ? parsedURL.href : url;
144144
const mock = mocks.get(baseURL);
145145

146+
const original = await nextLoad(url, context);
146147
debug('load hook, mock = %o', mock);
147148
if (mock?.active !== true) {
148-
return nextLoad(url);
149+
return original;
149150
}
150151

151152
// Treat builtins as commonjs because customization hooks do not allow a
152153
// core module to be replaced.
153-
const format = mock.format === 'builtin' ? 'commonjs' : mock.format;
154+
// Also collapse 'commonjs-sync' and 'require-commonjs' to 'commonjs'.
155+
const format = (
156+
original.format === 'builtin' ||
157+
original.format === 'commonjs-sync' ||
158+
original.format === 'require-commonjs') ? 'commonjs' : original.format;
154159

155-
return {
160+
const result = {
156161
__proto__: null,
157162
format,
158163
shortCircuit: true,
159-
source: await createSourceFromMock(mock),
164+
source: await createSourceFromMock(mock, format),
160165
};
166+
167+
debug('load hook finished, result = %o', result);
168+
return result;
161169
}
162170

163-
async function createSourceFromMock(mock) {
171+
async function createSourceFromMock(mock, format) {
164172
// Create mock implementation from provided exports.
165-
const { exportNames, format, hasDefaultExport, url } = mock;
173+
const { exportNames, hasDefaultExport, url } = mock;
166174
const useESM = format === 'module';
167175
const source = `${testImportSource(useESM)}
168176
if (!$__test.mock._mockExports.has('${url}')) {

0 commit comments

Comments
0 (0)
You are visitor 01402503 — 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.