Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


not.contain.keys(XX).and.contain.keys(YY) does not work as expected #256

andimeier opened this Issue · 3 comments

4 participants

Andi Meier Charlie Rudolph Arnaud Benhamdine Keith Cirkel
Andi Meier

I want to test this:


Let's assume a "green" case (meets the test criteria):

result = {
  info: 'Worked'

This works. If I combine both asserts into the single-line version:


it is still working.

But if swap both asserts and write it like this:


then I would assume that the test is still green but it tells me that:

Uncaught AssertionError: expected { Object (data, info) } to not contain key 'info'

Apparently the not keyword is still "in use" after the .and.. This seems counterintuitive to me. I think the .and. keyword should be used to chain separate assumptions together independently from each other. So my opinion is that the .and. keyword should "reset" any modifiers like ".not.".

What do you think?

BTW: a simpler case:


The first combination would yield "OK" whereas the second line would fail.

Charlie Rudolph

I'm not certain about resetting any modifiers but I like the idea that and resets the not modifier.

Arnaud Benhamdine

Had the same question : I paste a previous answer by @logicalparadox :

I think the misunderstanding comes from your expectation that Chai follows most/all English grammar rules. Unfortunately, English grammar has way too many rules (and exceptions to those rules) for it to be a reasonable undertaking to implement.

The challenge with designing Chai's chain-able assertions is finding the balance between being expressive and concise. Even if full grammar wasn't a daunting task to implement and document, it would make the API less concise, which is not good for a testing environment.

RULE: Any "flag" which modifies the behavior of an assertion (negation not or inclusion include/contain, etc...), once set to true should remain true until the end of the chain.
This effectively means that when concatenating multiple assertions into a single chain you should always put those assertions which you expect to be true before those you want to negate.

expect([ 'bar' ])'array').and.not.empty;

Also, one thing to note is that to, be, and, in addition to a few others are convenience noop() chains. As their only goal is to aid us humans, they can be omitted without side-effect.

/// same as above
expect([ 'bar' ]).an('array').not.empty;

In short, the behavior you are observing is intended, but once you get used to Chai's rudimentary grammar rules you will find that the power of concise wins over the convenience of expressive, especially when testing.

Keith Cirkel

Thanks for the issue @andimeier, and feel free to keep them coming, but in this case @abenhamdine has summed this up nicely. I'm going to close this, but if you have any other thoughts on it feel free to add them here.

Keith Cirkel keithamus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.