温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/test/default/MockTracker/fn
点击访问原文链接

MockTracker.fn method | Node.js node:test module | Bun

MockTracker.fn method | Node.js node:test module | BunBuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/fnMfn

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/fnMfn

method

test.default.MockTracker.fn { let cnt = 0; function addOne() { cnt++; return cnt; } function addTwo() { cnt += 2; return cnt; } const fn = t.mock.fn(addOne, addTwo, { times: 2 }); assert.strictEqual(fn(), 2); assert.strictEqual(fn(), 4); assert.strictEqual(fn(), 5); assert.strictEqual(fn(), 6); }); ```" data-algolia-static="false" data-algolia-merged="false" data-type="Method">fnF extends Function = (...args: any[]) => undefined>(original?: F,options?: MockFunctionOptions): MockF>;

This function is used to create a mock function.

The following example creates a mock function that increments a counter by one on each invocation. The times option is used to modify the mock behavior such that the first two invocations add two to the counter instead of one.

test('mocks a counting function', (t) => {
let cnt = 0;

function addOne() {
cnt++;
return cnt;
}

function addTwo() {
cnt += 2;
return cnt;
}

const fn = t.mock.fn(addOne, addTwo, { times: 2 });

assert.strictEqual(fn(), 2);
assert.strictEqual(fn(), 4);
assert.strictEqual(fn(), 5);
assert.strictEqual(fn(), 6);
});
@param original

An optional function to create a mock on.

@param options

Optional configuration options for the mock function.

@returns

The mocked function. The mocked function contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked function.

fnF extends Function = (...args: any[]) => undefined, Implementation extends Function = F>(original?: F,implementation?: Implementation,options?: MockFunctionOptions): MockF | Implementation>;

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

MockTracker.fn method | Node.js node:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method node:test.default.MockTracker.fn | Bun