In many testing libraries it is possible to supply a custom message for a given expectation, this is currently not possible in Jest.
For example:
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:
Expect takes at most one argument.jest-expect-message allows you to call expect with a second argument of a String message.
For example the same test as above:
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});With jest-expect-message this will fail with your custom error message:
β returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2With npm:
npm install --save-dev jest-expect-messageWith yarn:
yarn add -D jest-expect-messageAdd jest-expect-message to your Jest setupFilesAfterEnv configuration.
See for help
"jest": {
"setupFilesAfterEnv": ["jest-expect-message"]
}"jest": {
"setupTestFrameworkScriptFile": "jest-expect-message"
}expect(actual, message)actual: The value you would normally pass into anexpectto assert against with a given matcher.message: String, the custom message you want to be printed should theexpectfail.
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});Matt Phillips π» π π‘ π€ π |
|---|