@semantic-matchers/jest
v0.1.1
Published
Customize expect with class-scoped matchers for Jest. Type-specific assertion dispatch on the prototype chain — chainable with .not, .resolves, and .rejects.
Maintainers
Keywords
Readme
@semantic-matchers/jest
Customize expect with class-scoped matchers for Jest. Type-specific assertion logic per class, resolved on the prototype chain — chainable with .not, .resolves, and .rejects.
Problem this solves
You might be looking for:
- Class-based / class-scoped matchers — different
toX()behavior per domain type - Type-specific custom matchers without
instanceofbranches in every matcher - Expect customization beyond global
expect.extend - Chainable custom assertions (
.not,.resolves,.rejects) on class instances - Page-object or domain matchers —
expect(user).toHaveEmail(),expect(button).toBeVisible() - Polymorphic / dynamic dispatch for Jest assertions
Jest matchers are global by default. This adapter scopes them to classes with full TypeScript inference.
Install
npm install -D @semantic-matchers/jest expect
# or
yarn add -D @semantic-matchers/jest expectSetup
jest.setup.ts
import '@semantic-matchers/jest/register-types';
import expect from 'expect';
import {installSemanticExpect} from '@semantic-matchers/jest';
installSemanticExpect(expect, {
global: true,
exposeOriginalAs: 'jestExpect',
});Usage
semanticExpect.extend(userMatchers, User);
const user = new User();
user.email = '[email protected]';
expect(user).toHaveEmail('[email protected]');
await expect(Promise.resolve(user)).resolves.toHaveEmail('[email protected]');
expect(user).not.toHaveEmail('[email protected]');Docs
Full documentation and examples: github.com/dvegap95/semantic-matchers
