-
-
Notifications
You must be signed in to change notification settings - Fork 697
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
Issues with contains
, any
, and all
#881
Comments
Thank you @meeber, as always, very detailed stuff! IMHO Chai could deprecate both About any: About all: // as @meeber explained:
expect(['cat', 'dog']).to.have.members(['cat']); // this fails, it wants ALL members
expect(['cat', 'dog']).to.contains.members(['cat']); // this passes, with contains you can match partially |
@vieiralucas Agreed except it's worth clarifying that Chai would always choose If the team agrees that
|
I totally agree with you @meeber! That was a great explanation! As you've said it yourself, +1 for deprecation. |
Pinging @shvaikalesh and @keithamus :D |
I still think we need to do something here: possibly dump Arguments in favor of keeping
Argument in favor of dumping
|
In the process of rewriting some documentation for an unrelated PR, I ran into a bug that's related to this topic. Because of this line (I presume), the following tests both fail: expect({a: 1, b: 2, c: 3}).to.have.keys('a', 'b'); // Correctly fails
expect({a: 1, b: 2, c: 3}).to.not.have.keys('a', 'b'); // Should pass but it fails too The second test is incorrectly failing; it should pass right away on the basis that the target and given values don't have the same number of keys, which is a requirement when the |
I think we have a good implicit plan for chai 5 where the architecture will change to drop flags, and instead you'll only be able to use greenlit chains of words for assertions:
We can keep discussing this, if you have any input - but right now the above points are reflected in the roadmap. I'll close this though, as I want us to move to closing issues when we have something actionable that can sit in the roadmap - which I think we now do. |
While looking at #880, I had a couple of thoughts regarding
contains
,any
, andall
that were beyond the scope of that PR:contains
consistent across assertions.any
andall
becauseany
encourages bad practices. But if we don't deprecate them, then we should keep their behavior consistent across assertions too.Before I get in to the second point, I'll outline the various usages of
contains
,any
, andall
. Two points of clarification: 1)contains
andincludes
are synonymous; I'll only usecontains
for consistency. And 2) Althoughhave
is sometimes used in our documentation as being the opposite ofcontains
,have
isn't actually a flag in Chai, and doesn't have any actual functionality; it's just a language chain. From a technical perspective, the only thing that matters is whether or not thecontains
flag is set.Usage 1:
contains
can be used as a method assertion to assert inclusions of a value in an array, a substring in a string, or a subset of properties in an object.any
orall
flags.Usage 2:
keys
asserts that a target object/map/set has any or all of the given keys.any
,all
, andcontains
flags.any
, doesn't matter ifcontains
is set or not; asserts that target has at least one of the given keys.all
andcontains
, target must have all the given keys, but is allowed to have more.all
but notcontains
, target must have all of the given keys and no others.all
if neitherany
norall
is specified.Usage 3:
members
asserts that a target array has or includes the same members as the given array.contains
flag. Doesn't interact withany
orall
flags.contains
, target must have all the given members, but is allowed to have more.contains
, target must have all the given members and no others.contains
.Usage 4: Per #880,
strings
asserts that a target string contains the given strings.any
andall
flags. Doesn't interact withcontains
flag.any
, asserts that target has at least one of the given strings.all
, asserts that target must have all the given strings, but is allowed to have more.all
if neitherany
orall
is specified.The reason the
any
flag encourages bad testing habits is because it introduces uncertainty into the test much in the same way that adding anor
oreither
assertion to Chai would. See this comment for my objections toor
andeither
. As forany
, consider this statement: "I expect an array to containany
of these values: blue, green, and/or red". Why doesn't the tester know exactly what the array contains, and test exactly for that, instead of testing for a list of possible values? Is the test non-deterministic, or is it being reused for multiple assertions? I haven't been able to think of a situation when it's a good idea to useany
.The text was updated successfully, but these errors were encountered: