javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Document best practices for test assertions

Open zeke opened this issue 9 years ago • 4 comments

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.

zeke avatar Dec 29 '15 17:12 zeke

@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?

Ensive avatar Feb 27 '19 17:02 Ensive

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.

ljharb avatar Feb 27 '19 18:02 ljharb

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 avatar Feb 28 '19 04:02 zeke

@zeke this config has protected you from that for 3.5 years.

ljharb avatar Feb 28 '19 04:02 ljharb

This issue can also be closed.

postmeback avatar May 11 '23 11:05 postmeback