MockPropertyContext.mockImplementationOnce method | Node.js node:test module | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:test/
default/
MockPropertyContext/
mockImplementationOnceMmockImplementationOnce
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:test/
default/
MockPropertyContext/
mockImplementationOnceMmockImplementationOnce
method
test.default.MockPropertyContext.mockImplementationOnce {
const obj = { foo: 1 };
const prop = t.mock.property(obj, 'foo', 5);
assert.strictEqual(obj.foo, 5);
prop.mock.mockImplementationOnce(25);
assert.strictEqual(obj.foo, 25);
assert.strictEqual(obj.foo, 5);
});
```" data-algolia-static="false" data-algolia-merged="false" data-type="Method">
mockImplementationOnce(value: PropertyType,onAccess?: number): void;
This function is used to change the behavior of an existing mock for a single invocation. Once invocation onAccess has occurred, the mock will revert to whatever behavior it would have used had mockImplementationOnce() not been called.
The following example creates a mock function using t.mock.property(), calls the mock property, changes the mock implementation to a different value for the next invocation, and then resumes its previous behavior.
test('changes a mock behavior once', (t) => {
const obj = { foo: 1 };
const prop = t.mock.property(obj, 'foo', 5);
assert.strictEqual(obj.foo, 5);
prop.mock.mockImplementationOnce(25);
assert.strictEqual(obj.foo, 25);
assert.strictEqual(obj.foo, 5);
});
@param value
The value to be used as the mock's implementation for the invocation number specified by onAccess.
@param onAccess
The invocation number that will use value. If the specified invocation has already occurred then an exception is thrown. Default: The number of the next invocation.
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →