expect.extend() : is a utility allows you to add custom matchers and use them on any unit test in the file.test.js.
expect.extend({
toBeJest(received) {
const pass = received === `Jest`;
if (pass) {
return {
message: () => 'The received is Jest',
pass: true
};
} else {
return {
message: () => `Error: The received ${received} is not Jest`,
pass: false
};
}
}
});
it('Testing toBeJest Matcher...', () => {
expect(sayJest()).toBeJest();
});Any matcher in Jest returns an object has two fundamental properties message which is a function returns a string message, and pass which is a truth value indicates whether the test passed or failed.
Note: the
expect().notmodifier negates thepassproperty by returning its bang!pass.
Note: the
receivedobject doesn't belong to the matcher method explicitly but, belongs to the return ofexpect(something)as it's the return value of thesomething. you can add a parameter(ex: target)to your matcher method that mean your matcher accepts one parameter calledtarget.