-
-
Notifications
You must be signed in to change notification settings - Fork 32k
test_runner: support module mocking #52848
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1775,6 +1775,25 @@ added: | |
Resets the implementation of the mock function to its original behavior. The | ||
mock can still be used after calling this function. | ||
|
||
## Class: `MockModuleContext` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1.0 - Early development | ||
|
||
The `MockModuleContext` class is used to manipulate the behavior of module mocks | ||
created via the [`MockTracker`][] APIs. | ||
|
||
### `ctx.restore()` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
Resets the implementation of the mock module. | ||
|
||
## Class: `MockTracker` | ||
|
||
<!-- YAML | ||
|
@@ -1908,6 +1927,68 @@ test('spies on an object method', (t) => { | |
}); | ||
``` | ||
|
||
### `mock.module(specifier[, options])` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1.0 - Early development | ||
|
||
* `specifier` {string} A string identifying the module to mock. | ||
* `options` {Object} Optional configuration options for the mock module. The | ||
following properties are supported: | ||
* `cache` {boolean} If `false`, each call to `require()` or `import()` | ||
generates a new mock module. If `true`, subsequent calls will return the same | ||
module mock, and the mock module is inserted into the CommonJS cache. | ||
**Default:** false. | ||
* `defaultExport` {any} An optional value used as the mocked module's default | ||
export. If this value is not provided, ESM mocks do not include a default | ||
export. If the mock is a CommonJS or builtin module, this setting is used as | ||
the value of `module.exports`. If this value is not provided, CJS and builtin | ||
mocks use an empty object as the value of `module.exports`. | ||
* `namedExports` {Object} An optional object whose keys and values are used to | ||
create the named exports of the mock module. If the mock is a CommonJS or | ||
builtin module, these values are copied onto `module.exports`. Therefore, if a | ||
mock is created with both named exports and a non-object default export, the | ||
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. The following example demonstrates how a mock | ||
is created for a module. | ||
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 " Any references to the original module prior to mocking are not impacted." part probably needs emphasizing (that users need to dynamically import their function under test and ensure it was not imported (and resolved its imports) prior to t.mock.module ) |
||
|
||
```js | ||
test('mocks a builtin module in both module systems', async (t) => { | ||
// Create a mock of 'node:readline' with a named export named 'fn', which | ||
// does not exist in the original 'node:readline' module. | ||
const mock = t.mock.module('node:readline', { | ||
namedExports: { fn() { return 42; } }, | ||
}); | ||
|
||
let esmImpl = await import('node:readline'); | ||
let cjsImpl = require('node:readline'); | ||
|
||
// cursorTo() is an export of the original 'node:readline' module. | ||
assert.strictEqual(esmImpl.cursorTo, undefined); | ||
assert.strictEqual(cjsImpl.cursorTo, undefined); | ||
assert.strictEqual(esmImpl.fn(), 42); | ||
assert.strictEqual(cjsImpl.fn(), 42); | ||
|
||
mock.restore(); | ||
|
||
// The mock is restored, so the original builtin module is returned. | ||
esmImpl = await import('node:readline'); | ||
cjsImpl = require('node:readline'); | ||
|
||
assert.strictEqual(typeof esmImpl.cursorTo, 'function'); | ||
assert.strictEqual(typeof cjsImpl.cursorTo, 'function'); | ||
assert.strictEqual(esmImpl.fn, undefined); | ||
assert.strictEqual(cjsImpl.fn, undefined); | ||
}); | ||
``` | ||
|
||
### `mock.reset()` | ||
|
||
<!-- YAML | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.