Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'truthy' and 'falsy' should be chainable properties. #235

Closed
jamestalmage opened this issue Jan 24, 2014 · 11 comments
Closed

'truthy' and 'falsy' should be chainable properties. #235

jamestalmage opened this issue Jan 24, 2014 · 11 comments

Comments

@jamestalmage
Copy link

Especially since the description for ok actually uses the word truthy.

@jamestalmage
Copy link
Author

I'm happy to submit a PR if people are amenable to the idea.

@logicalparadox
Copy link
Member

This seems redundant for core as x.should.be.truthy would be the same as x.should.be.ok AND x.should.be.falsy would be the same as x.should.not.be.ok. Feel free to implement as plugin.

@jamestalmage
Copy link
Author

Consider this fairly common scenario: Some API's return a result object which may or may not have a property error. The presence of said property indicates that there was .... an error. So while writing a test where I expect an error, I might do something like this:

var result = someActionThatShouldProduceAnError();
expect(result.error).to.be.ok;

That is potentially confusing to someone who isn't familiar with chai. The following is easier to understand.

expect(result.error).to.be.truthy

If you google "javascript ok" vs. "javascript truthy", I think it shows that truthy is a better choice than ok.

Chai is full of redundant syntax, so rejection on that basis seems arbitrary.

  • above, gt, greaterThan
  • equal, equals, eq
  • include, contain

I could be persuaded to give up on falsy in favor of not.truthy, but I'd ask you to reconsider rejecting the idea in its entirety.

@kristianfreeman
Copy link

Realize this is closed, but giving this a 👍 because I also think that "truthy"/"falsy" is more clear than "ok".

@juuxstar
Copy link

OMG +1 for this. just ran into this as well. We've been using truthy and falsy throughout our tests and just discovered that they're not actually passing or failing. And chai doesn't complain at expect(...).to.be.truthy. I would imagine it should at least throw some type of WTF is truthy error. Talk about principle of most surprise.

@franciscop
Copy link

@jamestalmage came exactly to comment on the same. On my code:

expect(res.error).to.be.not.ok;

Where this would be a lot more clear:

expect(res.error).to.be.falsy;

@mikeabiezzi
Copy link

@logicalparadox +1 on the intuitiveness of .to.be.truthy and .to.be.falsy

@epb-644
Copy link

epb-644 commented Mar 7, 2016

+1 we've had a lot of confusion on this in our codebase

thughes added a commit to airtimemedia/eslint-plugin-chai-expect that referenced this issue Oct 6, 2017
Accidentally using undefined terminating properties leads to the expect
check not working as expected. For example,

expect(foo).to.be.falsy

results in a no-op that does not perform any check and is probably not
what the author intended. See chaijs/chai#235
for a discussion of "truthy" and "falsy", including examples of people that
have run into this:
chaijs/chai#235 (comment)
thughes added a commit to airtimemedia/eslint-plugin-chai-expect that referenced this issue Oct 10, 2017
Accidentally using undefined terminating properties leads to the expect
check not working as expected. For example,

expect(foo).to.be.falsy

results in a no-op that does not perform any check and is probably not
what the author intended. See chaijs/chai#235
for a discussion of "truthy" and "falsy", including examples of people that
have run into this:
chaijs/chai#235 (comment)
@lonix1
Copy link

lonix1 commented Oct 28, 2021

Also bitten by this. If someone actually finds a plugin/workaround for this, please let the rest of us know? Thanks!

@PatrikValkovic
Copy link

I would also like to ask for this feature and reopen the issue. Writing to.be.falsy instead of not.to.be.ok makes sense (especially for error checking as mentioned above) and as it should be easily implemented above not.ok, I don't see the reason why not to include it in the library. Especially when chai targets the readability with its be, to, should, etc., it makes perfect sense to use falsy and truthy as well.
@logicalparadox

@JakobJingleheimer
Copy link

@logicalparadox there are several instances of assertion aliases already in Chai. This seems a pretty reasonable alias. I have to google "chaijs truthy" (or "chaijs falsy") every time.

On a related note: "ok" is very unclear. "ok" literally means "all right" (as in, "no problems"), so one would reasonably see "ok" and think "doesn't throw".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants