javascript
javascript copied to clipboard
Document best practices for test assertions
There's an issue on enzyme https://github.com/airbnb/enzyme/issues/97 about strengthening the assertion styles to avoid false positives:
A great first task would be removing this rule override, and fixing our tests and docs to use, for example, .to.equal(true) instead of .to.be.true
The issue mentions that the to.be.true
style violates this styleguide, but I wasn't able to find any content here that alludes to this.
@ljharb How actual this issue is? Do you think we need to add something like:
Do not use
to.be.true
, etc. in your tests, but use.to.equal(true)
Shall we put it within Testing section?
Yes, but specifically, a section on never using "noop" matchers - ie, anything that doesn't end in a function call - because the cost of a typo is a silent passing test. In other words, expect(x).to.be.yogurt;
will merrily pass, whereas expect(x).to.be.yogurt()
will correctly throw, failing your test.
Thanks for following up on this, @ljharb 👍
Since opening this issue (over three years ago!), the standard
linter has been updated to no longer allow expressions like to.be.true
, which has protected me from accidentally using that assertion style in projects.
@zeke this config has protected you from that for 3.5 years.
This issue can also be closed.