jest: Custom error messages for assertions
I’m guessing this has already been brought up, but I’m having trouble finding the issue.
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
expect(false).toBe(true, "it's true")
doesn’t print “it’s true” in the console output.
If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install
and yarn test
.
What is the expected behavior?
The message should be included in the response somehow.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Just made a new project
yarn add --dev jest
echo 'it("works", () => { expect(true).toBe(false, "FOO"); });' > a.test.js
./node_modules/.bin/jest
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 55
- Comments: 22 (9 by maintainers)
sigh… ok: so its possible to include custom error messages. Still (migrating from mocha), it does seem quite inconvenient not to be able to pass a string in as a prefix or suffix. I would think this would cover many common use cases – in particular
expect()
in loops or in a subroutine that is called more than once.UPDATE
Ok … not to undercut the case, but a workaround is changing
expect(result).toEqual(expected)
to:Why was this closed? The linked discussion doesn’t mention custom error messages! Is this supported in jest?
@cpojer @SimenB I get that it’s not possible to add a message as a last param for every assertion. But what about very simple ones, like
toBe
andtoEqual
? I think that would cover 99% of the people who want this.toHaveProperty
will already give very readable error messages. When I usetoBe
andtoEqual
it’s usually because I have some custom condition that jest can’t easily help me assert on out-of-the-box. It’s especially bad when it’s something likeexpected "true", got "false"
.Can we reduce the scope of this request to only
toBe
andtoEqual
, and from there consider (or not consider) other assertion types?There are many questions here, one of them in this issue #1965.
toBe and toEqual would be good enough for me. Personally I really miss the ability to specify a custom message from other packages like chai.
expected 0 to equal 1
usually means I have to dig into the test code to see what the problem was. I would appreciate this featurePosting this here incase anyone stumbles across this issue 😄
jest-expect-message allows custom error messages for assertions.
Use
assert
instead ofexpect
is the current workaround if you really need itFor those of you who don’t want to install a package, here is another solution with
try/catch
:Code
How it looks
Pull Request for Context https://github.com/Human-Connection/Human-Connection/pull/1553
Code to copy+paste
I want to show a custom error message only on rare occasions, that’s why I don’t want to install a package. In our case it’s a helpful error message for
dummiesnew contributors.You can also throw an error following way, without using
expect()
:It comes handy if you have to deal with a real async code, like bellow:
@cpojer is there a way to produce custom error messages?
I’m using lighthouse and puppeteer to perform an automated accessibility audit. I don’t know beforehand how many audits are going to be performed and lighthouse is asynchronous so I can’t just wrap each audit result in the response in a test block to get a useful error message.
I’m left with
Which then returns
So if I have a single audit failure I just get expected whatever to be true, it was false but with no information as to which audit failed.
This is the only way I could think of to get some useful output but it’s not very pretty.
fwiw: it works well if you don’t use
flow
for type checkingWhen you have promises, it’s highly recommended to
return
them.That will behave the same as your example
@SimenB that worked really well. The whole puppeteer environment element was overkill for my needs as not all the tests require it but here’s what I used.
Thanks @mattphillips, your
jest-expect-message
package works for me! Especially when you have expectations in loops, this functionality is really important.All of the above solutions seem reasonably complex for the issue. besides rolling the message into an array to match with
toEqual
, which creates (in my opinion) ugly output.In that spirit, though, I’ve gone with the simple:
Jest’s formatting of
console.log()
s looks reasonably nice, so I can easily give extra context to the programmer when they’ve caused a test to fail in a readable manner. Logging plain objects also creates copy-pasteable output should they havenode
open and ready.I just use
chai.should
:__setup.js:
in a test:
When things like that fail the message looks like:
AssertionError: result.URL did not have correct value: expected { URL: 'abc' } to have property 'URL' of 'adbc', but got 'abc'
Which, to me, is much nicer.
Looks like a
node
thing.@SimenB ok ty! it worked.