test_runner: support mocking json modules · nodejs/node@99e4685 · GitHub
Skip to content

Commit 99e4685

Browse files
JakobJingleheimerRafaelGSS
authored andcommitted
test_runner: support mocking json modules
PR-URL: #58007 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 77d11f9 commit 99e4685

5 files changed

Lines changed: 57 additions & 9 deletions

File tree

doc/api/test.md

Lines changed: 9 additions & 4 deletions

lib/internal/test_runner/mock/loader.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,17 @@ async function load(url, context, nextLoad) {
118118
// Treat builtins as commonjs because customization hooks do not allow a
119119
// core module to be replaced.
120120
// Also collapse 'commonjs-sync' and 'require-commonjs' to 'commonjs'.
121-
const format = (
122-
original.format === 'builtin' ||
123-
original.format === 'commonjs-sync' ||
124-
original.format === 'require-commonjs') ? 'commonjs' : original.format;
121+
let format = original.format;
122+
switch (original.format) {
123+
case 'builtin': // Deliberate fallthrough
124+
case 'commonjs-sync': // Deliberate fallthrough
125+
case 'require-commonjs':
126+
format = 'commonjs';
127+
break;
128+
case 'json':
129+
format = 'module';
130+
break;
131+
}
125132

126133
const result = {
127134
__proto__: null,

lib/internal/test_runner/mock/mock.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ const kMockUnknownMessage = 3;
7070
const kWaitTimeout = 5_000;
7171
const kBadExportsMessage = 'Cannot create mock because named exports ' +
7272
'cannot be applied to the provided default export.';
73-
const kSupportedFormats = ['builtin', 'commonjs', 'module', 'module-typescript', 'commonjs-typescript'];
73+
const kSupportedFormats = [
74+
'builtin',
75+
'commonjs-typescript',
76+
'commonjs',
77+
'json',
78+
'module-typescript',
79+
'module',
80+
];
7481
let sharedModuleState;
7582

7683
class MockFunctionContext {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"foo":"bar"}

test/parallel/test-runner-module-mocking.js

Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)