Add custom message to Jest expects
npm install jest-27-expect-messagejest-expect-message.
jest-expect-message will throw this error when used in newer versions of Jest:
"TypeError: matcherResult.message is not a function"
`
Version 1.0.4 is fully compatible with jest-expect-message. Starting in version 1.1.0, I'll start including new features and improvement.s
If jest-expect-message is updated to support Jest 27+, this package will be marked as deprecated.
js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
`
This will throw the following error in Jest:
`sh
Expect takes at most one argument.
`
Solution
jest-27-expect-message allows you to call expect with a second argument of a String message.
For example the same test as above:
`js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
`
With jest-27-expect-message this will fail with your custom error message:
`sh
ā returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2
`
Installation
With npm:
`sh
npm install --save-dev jest-27-expect-message
`
With yarn:
`sh
yarn add -D jest-27-expect-message
`
Setup
Add jest-27-expect-message to your Jest setupFilesAfterEnv configuration.
See for help
$3
`json
"jest": {
"setupFilesAfterEnv": ["jest-27-expect-message"]
}
`
$3
`json
"jest": {
"setupTestFrameworkScriptFile": "jest-27-expect-message"
}
`
Usage
- expect(actual, message)
- actual: The value you would normally pass into an expect to assert against with a given matcher.
- message: String, the custom message you want to be printed should the expect fail.
`js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
``