Register new custom matchers.
Expect.extend method | bun:test module | Bun BuildDocsReferenceGuidesBlogDiscord/bun:test/Expect/extendMextend BuildDocsReferenceGuidesBlogDiscord /bun:test/Expect/extendMextend test.Expect.extendextendM>(matchers: ExpectExtendMatchersM>): void; @param matchers Referenced typestype ExpectExtendMatchersM> = { [K in keyof M]: k extends keyof CustomMatchersDetected ? CustomMatcherunknown, ParametersCustomMatchersDetected[k]>> : CustomMatcherunknown, any[]> }
Search the reference...
/
method
An object containing the matchers to register, where each key is the matcher name, and its value the implementation function. The function must satisfy: (actualValue, ...matcherInstantiationArguments) => { pass: true|false, message: () => string }
expect.extend({
toBeWithinRange(actual, min, max) {
if (typeof actual !== 'number' || typeof min !== 'number' || typeof max !== 'number')
throw new Error('Invalid usage');
const pass = actual >= min && actual max;
return {
pass: pass,
message: () => `expected ${this.utils.printReceived(actual)} ` +
(pass ? `not to be`: `to be`) + ` within range ${this.utils.printExpected(`${min} .. ${max}`)}`,
};
},
});
test('some test', () => {
expect(50).toBeWithinRange(0, 100); // will pass
expect(50).toBeWithinRange(100, 200); // will fail
expect(50).toBe(expect.toBeWithinRange(0, 100)); // will pass
expect(50).toBe(expect.not.toBeWithinRange(100, 200)); // will pass
});
If the types has been defined through declaration merging, enforce it. Otherwise enforce the generic custom matcher signature.
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlogToolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →Expect.extend method | bun:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引
- API documentation for method bun:test.Expect.extend | Bun