MediumPro challengePythonJavaScriptTypeScript
Implement expect.extend()
TestingUtilities
Jest lets you register custom matchers with expect.extend({ ... }), then use them like built-ins — including through .not.
The starter code provides the custom matchers in MATCHER_LIBRARY. Each takes (received, ...args) and returns { pass, message }, where message describes the failure.
solve(ops) runs a list of operations in order, starting from an empty registry, and returns their results:
| Op | Result |
|---|---|
['extend', names] | null — registers the named matchers from MATCHER_LIBRARY |
['match', name, received, ...args] | { pass, message } — runs a registered matcher |
['list'] | registered matcher names, in registration order, without duplicates |
Rules for match:
messageis''whenever the result passes.- A name prefixed with
not.inverts the matcher. If the inverted check fails, the message is'toBeEven unexpectedly passed for 4'. - A name that was never registered →
{ pass: false, message: 'Unknown matcher: toBeOdd' }.
solve([
['extend', ['toBeEven']],
['match', 'toBeEven', 3],
['match', 'not.toBeEven', 3],
])
// → [null, { pass: false, message: 'expected 3 to be even' }, { pass: true, message: '' }]Sample tests
Test #1a registered matcher passes
Input: [[["extend",["toBeEven"]],["match","toBeEven",4]]]
Output: [null,{"pass":true,"message":""}]
Test #2a registered matcher fails with its message
Input: [[["extend",["toBeEven"]],["match","toBeEven",3]]]
Output: [null,{"pass":false,"message":"expected 3 to be even"}]
Test #3list returns registered names in order
Input: [[["extend",["toBeEven","toBePositive"]],["list"]]]
Output: [null,["toBeEven","toBePositive"]]
Test #4unknown matcher
Input: [[["match","toBeOdd",3]]]
Output: [{"pass":false,"message":"Unknown matcher: toBeOdd"}]
Test #5.not inverts the matcher
Input: [[["extend",["toBeEven"]],["match","not.toBeEven",3],["match","not.toBeEven",4]]]
Output: [null,{"pass":true,"message":""},{"pass":false,"message":"toBeEven unexpectedly passed for 4"}]