-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
test_runner: support mocking json modules #58007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8961496
17764f7
32a4f02
4e1ed7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2168,6 +2168,11 @@ test('spies on an object method', (t) => { | |||||||||||||||||
added: | ||||||||||||||||||
- v22.3.0 | ||||||||||||||||||
- v20.18.0 | ||||||||||||||||||
changes: | ||||||||||||||||||
- version: | ||||||||||||||||||
- REPLACEME | ||||||||||||||||||
pr-url: https://github.com/nodejs/node/pull/58007 | ||||||||||||||||||
description: Support JSON modules. | ||||||||||||||||||
--> | ||||||||||||||||||
|
||||||||||||||||||
> Stability: 1.0 - Early development | ||||||||||||||||||
|
@@ -2191,10 +2196,10 @@ added: | |||||||||||||||||
mock will throw an exception when used as a CJS or builtin module. | ||||||||||||||||||
* Returns: {MockModuleContext} An object that can be used to manipulate the mock. | ||||||||||||||||||
|
||||||||||||||||||
This function is used to mock the exports of ECMAScript modules, CommonJS | ||||||||||||||||||
modules, and Node.js builtin modules. Any references to the original module | ||||||||||||||||||
prior to mocking are not impacted. In order to enable module mocking, Node.js must | ||||||||||||||||||
be started with the [`--experimental-test-module-mocks`][] command-line flag. | ||||||||||||||||||
This function is used to mock the exports of ECMAScript modules, CommonJS modules, JSON modules, and | ||||||||||||||||||
Node.js builtin modules. Any references to the original module prior to mocking are not impacted. In | ||||||||||||||||||
order to enable module mocking, Node.js must be started with the | ||||||||||||||||||
[`--experimental-test-module-mocks`][] command-line flag. | ||||||||||||||||||
Comment on lines
+2199
to
+2202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only change here was to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's minimize the diff
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
The following example demonstrates how a mock is created for a module. | ||||||||||||||||||
|
||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,10 +118,17 @@ async function load(url, context, nextLoad) { | |
// Treat builtins as commonjs because customization hooks do not allow a | ||
// core module to be replaced. | ||
// Also collapse 'commonjs-sync' and 'require-commonjs' to 'commonjs'. | ||
const format = ( | ||
original.format === 'builtin' || | ||
original.format === 'commonjs-sync' || | ||
original.format === 'require-commonjs') ? 'commonjs' : original.format; | ||
let format = original.format; | ||
switch (original.format) { | ||
case 'builtin': // Deliberate fallthrough | ||
case 'commonjs-sync': // Deliberate fallthrough | ||
case 'require-commonjs': | ||
format = 'commonjs'; | ||
break; | ||
case 'json': | ||
format = 'module'; | ||
Comment on lines
+128
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main change. Creating a nested ternary seemed messy, so I changed this to a switch. |
||
break; | ||
} | ||
|
||
const result = { | ||
__proto__: null, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,14 @@ const kMockUnknownMessage = 3; | |
const kWaitTimeout = 5_000; | ||
const kBadExportsMessage = 'Cannot create mock because named exports ' + | ||
'cannot be applied to the provided default export.'; | ||
const kSupportedFormats = ['builtin', 'commonjs', 'module', 'module-typescript', 'commonjs-typescript']; | ||
const kSupportedFormats = [ | ||
'builtin', | ||
'commonjs-typescript', | ||
'commonjs', | ||
'json', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
'module-typescript', | ||
'module', | ||
]; | ||
let sharedModuleState; | ||
|
||
class MockFunctionContext { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could use an existing fixtures JSON file (e.g. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"foo":"bar"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit