Registry / testing / add-matchers

add-matchers

JSON →
library0.6.2jsnpmunverified

A JavaScript library designed to provide a unified API for creating custom test matchers that are compatible across different versions of popular testing frameworks, specifically Jest and Jasmine (1.x and 2.x). This library addresses the varying native APIs for custom matcher registration, allowing developers to write matchers once and use them interchangeably across these environments. The current stable version is 0.6.2. The project appears to be in an abandoned state, with no new releases or significant activity since 2019, suggesting a lack of future feature development or proactive compatibility updates. Its primary differentiator is simplifying cross-framework matcher development, encouraging reusability within the testing community without needing to rewrite matchers for each framework version.

npm install add-matchers
INSTALL
IMPORT
SIG · ADD-MATCHERS
A
add-matchers
testingjavascriptv0.6.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

addMatchers
import { addMatchers } from 'add-matchers';
const { addMatchers } = require('add-matchers');
The library primarily uses ES module syntax in its documentation, though older Node.js projects might attempt CommonJS. Ensure your environment supports ESM for direct usage.
addMatchers.asymmetric
import { addMatchers } from 'add-matchers'; addMatchers.asymmetric(...);
import { asymmetric } from 'add-matchers';
Asymmetric matchers are exposed as a property of the main `addMatchers` function, not a separate named export.

Demonstrates how to define and use both standard and asymmetric custom matchers compatible with Jest and Jasmine, highlighting the argument order convention for matcher functions.

import { addMatchers } from 'add-matchers'; // Register your custom matchers in a setup file (e.g., setupTests.js or jest.setup.js) addMatchers({ toBeEvenNumber: function(received) { return { pass: received % 2 === 0, message: () => `Expected ${received} to be an even number` }; }, toBeOfType: function(type, received) { return { pass: Object.prototype.toString.call(received) === '[object ' + type + ']', message: () => `Expected ${received} to be of type ${type}` }; }, toContainItems: function(arg1, arg2, arg3, received) { // Important: Arguments are ordered (matcherArgs..., receivedValue) const allIncluded = received.indexOf(arg1) !== -1 && received.indexOf(arg2) !== -1 && received.indexOf(arg3) !== -1; return { pass: allIncluded, message: () => `Expected ${received} to contain items ${arg1}, ${arg2}, ${arg3}` }; } }); // Register asymmetric matchers addMatchers.asymmetric({ toBeFoo: function(value) { return { pass: value === 'foo', message: () => `Expected value to be 'foo'` }; }, toInclude: function(other, value) { return { pass: value.includes(other), message: () => `Expected '${value}' to include '${other}'` }; } }); // Example usage in your test file (e.g., my.test.js) describe('Custom matchers', () => { it('should check for even numbers', () => { expect(4).toBeEvenNumber(); expect(3).not.toBeEvenNumber(); }); it('should check for object type', () => { expect({}).toBeOfType('Object'); expect([]).not.toBeOfType('Object'); }); it('should check for multiple contained items', () => { expect([100, 14, 15, 2]).toContainItems(2, 15, 100); expect([1, 2, 3]).not.toContainItems(4, 5); }); }); describe('Custom asymmetric matchers', () => { it('should work with toEqual for object properties', () => { const any = expect; // In Jest, `expect` typically exposes `any` for asymmetric matchers expect({ key: 'foo', prop: 'bar' }).toEqual({ key: any.toBeFoo(), prop: any.toInclude('ar') }); expect({ key: 'baz', prop: 'qux' }).not.toEqual({ key: any.toBeFoo(), prop: any.toInclude('ar') }); }); });
Debug
Known issues
gotchaCustom matcher functions in `add-matchers` expect the `received` value (the argument passed to `expect()`) as the *last* argument. Any other matcher arguments (`arg1, arg2, ...`) precede it. This is a common source of bugs if developers assume `received` is the first argument, as is often the case with native Jest/Jasmine custom matchers.
fix
Always define your custom matcher functions with the `(arg1, arg2, ..., received)` signature, where `received` is the value passed to `expect()`.
affects: >=0.1.0
breakingThe `add-matchers` project has seen no releases or significant activity since 2019 (version 0.6.2). This means it is effectively abandoned, and may not receive updates for new features, bug fixes, or compatibility with future major versions of Jest or Jasmine. Users should be prepared to maintain compatibility patches themselves.
fix
For critical projects, consider forking the library to manage updates internally, or evaluate actively maintained alternatives if long-term support and active development are crucial.
affects: >=0.6.2
gotchaThis library does not ship with TypeScript type definitions (`.d.ts` files). TypeScript users will need to create ambient type declarations (`declare module 'add-matchers' { ... }`) to properly type their custom matchers and the `addMatchers` API, particularly if extending Jest's `expect` matchers.
fix
Create custom `d.ts` files for `add-matchers` and extend Jest/Jasmine's `Matchers` interface to provide type safety for your custom matchers.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: expect(...).toBeMyCustomMatcher is not a function
The `addMatchers` function was not called or executed before the tests that attempt to use the custom matcher, or the import path was incorrect.
fix
Ensure that `addMatchers` is called in a test setup file (e.g., `setupTests.js` or `jest.setup.js`) that is configured to run before your tests, or import and call it directly in the test file where the matcher is used.
Expected value to be X, received Y (and the actual and expected values are swapped or incorrect)
The custom matcher function's arguments were implemented in the incorrect order. `add-matchers` expects `(arg1, arg2, ..., received)` where `received` is the value from `expect(received)`.
fix
Review the custom matcher function signature and ensure the `received` value is the last argument, with all other matcher-specific arguments (`arg1`, etc.) preceding it. This mistake often leads to subtle logic errors rather than clear TypeError messages.
Upgrade
Version history
0.6.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
45 hits · last 30 days
node
38
OpenAI (training)
1
Resources
add-matchers — npm install add-matchers · libregistry