MockTracker.method method | Node.js node:test module | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:test/
default/
MockTracker/
methodMmethod
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:test/
default/
MockTracker/
methodMmethod
method
test.default.MockTracker.method {
const number = {
value: 5,
subtract(a) {
return this.value - a;
},
};
t.mock.method(number, 'subtract');
assert.strictEqual(number.subtract.mock.calls.length, 0);
assert.strictEqual(number.subtract(3), 2);
assert.strictEqual(number.subtract.mock.calls.length, 1);
const call = number.subtract.mock.calls[0];
assert.deepStrictEqual(call.arguments, [3]);
assert.strictEqual(call.result, 2);
assert.strictEqual(call.error, undefined);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, number);
});
```" data-algolia-static="false" data-algolia-merged="false" data-type="Method">
methodMockedObject extends object, MethodName extends string | number | symbol>(object: MockedObject,methodName: MethodName,options?:
MockFunctionOptions): MockedObject[MethodName] extends Function ?
Mockany[any]> : never;
This function is used to create a mock on an existing object method. The following example demonstrates how a mock is created on an existing object method.
test('spies on an object method', (t) => {
const number = {
value: 5,
subtract(a) {
return this.value - a;
},
};
t.mock.method(number, 'subtract');
assert.strictEqual(number.subtract.mock.calls.length, 0);
assert.strictEqual(number.subtract(3), 2);
assert.strictEqual(number.subtract.mock.calls.length, 1);
const call = number.subtract.mock.calls[0];
assert.deepStrictEqual(call.arguments, [3]);
assert.strictEqual(call.result, 2);
assert.strictEqual(call.error, undefined);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, number);
});
@param object
The object whose method is being mocked.
@param methodName
The identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.
@param options
Optional configuration options for the mock method.
@returns
The mocked method. The mocked method contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked method.
methodMockedObject extends object, MethodName extends string | number | symbol, Implementation extends Function>(object: MockedObject,methodName: MethodName,implementation: Implementation,options?:
MockFunctionOptions): MockedObject[MethodName] extends Function ?
MockImplementation | any[any]> : never;
methodMockedObject extends object>(object: MockedObject,methodName: keyof MockedObject,options:
MockMethodOptions):
MockFunction>;
methodMockedObject extends object>(object: MockedObject,methodName: keyof MockedObject,implementation: Function,options:
MockMethodOptions):
MockFunction>;
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →