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
muslnode 18–226 runs
build_error
glibcnode 18–226 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')
});
});
});
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.
fixEnsure 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)`.
fixReview 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.
Audit
Dependencies
No dependency data recorded yet.