airbnb / javascript

JavaScript Style Guide
MIT License
144.77k stars 26.45k forks source link

Document best practices for test assertions #646

Closed zeke closed 1 year ago

zeke commented 8 years ago

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.

Ensive commented 5 years ago

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

ljharb commented 5 years ago

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.

zeke commented 5 years ago

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.

ljharb commented 5 years ago

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

postmeback commented 1 year ago

This issue can also be closed.