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

.keys assertion error message doesn't print all keys of the input object #1103

Closed
chriswatrous opened this issue Dec 5, 2017 · 2 comments
Closed

Comments

@chriswatrous
Copy link

chriswatrous commented Dec 5, 2017

This is related to #956.

If the input object is small enough it prints the whole object:

> expect({a:1, b:2, c:3, d:4, e:5}).to.have.keys(['a', 'b', 'qwer'])

AssertionError: expected { a: 1, b: 2, c: 3, d: 4, e: 5 } to have keys 'a', 'b', and 'qwer'

But with 7 or more keys it truncates it:

> expect({a:1, b:2, c:3, d:4, e:5, f:6, g:7}).to.have.keys(['a', 'b', 'qwer'])

AssertionError: expected { Object (a, b, ...) } to have keys 'a', 'b', and 'qwer'

It would be nice if it either printed a much larger object than just 7 keys, or it specifically listed the keys that were missing or unexpected, like:

> expect({a:1, b:2, c:3, d:4, e:5, f:6, g:7}).to.have.keys(['a', 'b', 'qwer'])

AssertionError: expected { Object (a, b, ...) } to have keys 'a', 'b', and 'qwer' but was missing key 'qwer' and had unexpected keys 'c', 'd', 'e', 'f', and 'g'.

My project is using 3.5.0 but It also behaves this way in 4.1.2

@meeber
Copy link
Contributor

meeber commented Dec 6, 2017

@chriswatrous There's a ton of room for improvement regarding failed assertion messages. It's on the road map to make things better in this regard (see #457 and #585). With that said, what test runner are you using? When I tried your example with Mocha, I get the same error message as you plus the diff which shows the missing and unexpected keys:

AssertionError: expected { Object (a, b, ...) } to have keys 'a', 'b', and 'qwer'
+ expected - actual

   [
     "a"
     "b"
  -  "c"
  -  "d"
  -  "e"
  -  "f"
  -  "g"
  +  "qwer"
   ]

Regarding the error message truncation specifically, you can set chai.config.truncateThreshold = 0; at the top of your tests, which results in:

AssertionError: expected { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7 } to have keys 'a', 'b', and 'qwer'

(The default threshold is 40 characters; 0 disables it, but you could increase to higher number.)

Gonna close this issue because there already seems to be workarounds for this specific issue, and there are already tickets tracking the messaging problems in general.

@chriswatrous
Copy link
Author

Thanks for the workaround.

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

2 participants