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

What to expect when you're expecting #501

Closed
astorije opened this issue Jul 25, 2015 · 5 comments
Closed

What to expect when you're expecting #501

astorije opened this issue Jul 25, 2015 · 5 comments

Comments

@astorije
Copy link
Member

I was making tests trying to be right on #496 (:smile:) and found that if you assert:

expect(3).to.include(2);

You end up with:

     TypeError: undefined is not a function
      at Assertion.include (lib/chai/core/assertions.js:217:30)
      at Assertion.assert (lib/chai/utils/addChainableMethod.js:83:49)
      at Context.<anonymous> (test/expect.js:1131:18)

The assert.include(3, 2); twin assertion reacts similarly, but the key difference is that the assert.include(haystack, needle) method definition explicitly says that the haystack should be {Array|String} (so one just needs to read the doc). The type of the argument given to expect() is never specified in the doc (or at least I couldn't find it), meaning this should Mixed.

Making sure the type is controlled for expect(...).to.include(...) is similar to issue #491 and PR #496.

I am guessing there are other cases like that but I haven't tested them all. I am not sure if there should be a global way to do that in the code or if we should handle that on a case-by-case basis as what we are leaning to on #496.

@keithamus
Copy link
Member

Good call as usual @astorije. We should check for other primitives in assertions like these.

I think for these kind of assertions we should probably throw an error explaining the problem. In this instance maybe AssertionError: expected 3 to include 2, but cannot include multiple values or something. Thoughts?

@astorije
Copy link
Member Author

What do you mean by cannot include multiple values?

I would go for something simple and explicit like AssertionError: argument of expect() must be an Array or a String, number given (or 3 given).
It's not necessary or useful to remind the value (2) as this is not causing any error and therefore can be misleading.

What do you think?

(Depending on when we reach a decision and how much work is needed/how much time I have, I'll see if I can work on a PR or not)

@keithamus
Copy link
Member

Agree, yours sounds much better than mine. 3 given I think would be best. PR welcome 😄

@astorije
Copy link
Member Author

Okay, I'll see if I can find an hour to send you that within the next few of days. I'd personally prefer [type] given as a user, but it's up to you.

@berkerpeksag
Copy link

This is also fixed in d7d9420 (just pasting the merge commit) and can be closed now.

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

No branches or pull requests

3 participants