-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
expect(null).to.be.within(0,10) does not fail assertion #691
Comments
Hi @helfer, thanks for the report! As a workaround, I'd recommend the following form: expect(null).to.be.a('number').within(0,1) I would worry about the implications of rejecting non-numbers here. Your example returns But what about other types of coercion? These valid tests would start failing if only numbers were allowed through:
So what we might actually be talking about here is allowing some types of per-spec coercions to occur, but not others. I dunno. It feels like a tough sell, but at the same time I agree it's disorienting for Thoughts? |
@meeber Thanks, the workaround makes sense, I'll use that. The main reason I was surprised by this behavior is that all the examples in the documentation use numbers, and do not suggest that within coerces other types. I know Javascript is quite loose when it comes to comparators, but I think most people would expect a testing library to be much less tolerant. To keep backwards compatibility, it's probably best not to change the behavior, but the documentation should be updated to mention that this works with other types and that a('number') should be used if a number is expected. |
Agreed. If we move forward with the current functionality, then the documentation should indicate so and include examples of coercion. Additionally, tests should be added demonstrating the coercion. Going to leave this issue open for awhile for further feedback and possible pull-request-wanted for docs/tests update. |
We could definitely make |
@keithamus I think changing |
I think it's reasonable for an assertion library to avoid coercion games and demand specific types. I'd throw my support behind this as a breaking change for 4.0.0 assuming the following:
I also wonder if it'd be useful to have an assertion that behaved similarly to |
Anyone interested in working the PR for this? @helfer? :D |
@meeber Not sure I'm familiar enough with the conventions and internals of chai, but with the proper guidance I could take a stab at it. |
Hi @helfer, Whenever you run into something from Basically you will have to run some checks for types inside the expressions @meeber said above, but you may tackle this the way you think it's better. If you need to know more about the contribution process we've got this file too. Please let me know if you need anything, I would certainly be eager to help you contribute and earn a spot into our Hall of Fame 😄 |
It looks like the And it already has tests for the type check here, here, and here Therefore, one approach is to use the existing code for |
Was thinking about this earlier. One possibility would be to allow Examples: expect(3).to.be.within(2, 4); // Pass
expect("b").to.be.within("a", "c"); // Pass
expect(3).to.be.within("a", "c"); // Type error
expect("b").to.be.within(2, 4); // Type error Possible aliases: expect("z").to.be.above("a"); // Original
expect("z").to.come.after("a"); // Alias?
expect("z").to.follow("a"); // Alias?
expect("a").to.be.below("z"); // Original
expect("a").to.come.before("z"); // Alias?
expect("a").to.precede("z"); // Alias?
expect("m").to.be.within("a", "z"); // Original
expect("m").to.come.between("a", "z"); // Alias? |
@meeber while that would be a cool idea, I think its perhaps a draft for another issue. For now, let's do type checking to make sure the expectation is a number, and we can extend to strings later on. |
Hello guys, I've been following chai's project for quite some time, and I think that I might be capable of making a pull request addressing this issue. |
@vieiralucas It's totally fine with me, and I don' think anyone else would mind, so go ahead! |
@vieiralucas Absolutely. In fact, it's on Chai's roadmap for 7.x.x to claim all of the world's Lucases as contributors. With your contribution, we'll be up to two! :D If you have any questions, doesn't hesitate to ask. Also, please keep the PR in scope of what was agreed upon earlier in the thread (i.e., numbers-only). |
@meeber hahaha Just to make sure, I should submit this to the 4.x.x branch right? |
@vieiralucas Correct! |
Leaving this open as a reminder that @vieiralucas's first PR resolved half of this issue, and that he'll submit a PR to resolve the other half after 4.0.0 lands. |
Expected behavior:
expect(null).to.be.within(0,1)
should return false for any numbers a and b.Actual behavior:
expect(null).to.be.within(0,1)
returns true. Sam for other falsey values, I assume, and similar problem with true.Proposed fix:
Within should check that the value is a number and reject if it isn't before comparing the range here.
The text was updated successfully, but these errors were encountered: