expect: .toExcludeKeys not working as expected

Hey,

thanks for this great library.

I just came across .toExcludeKeys. And for me it feels that something is wrong here. The docs say:

does not contain any of the provided keys

Which sounds to me, if there is at least one key in the provided keys which is contained in the given object, it should fail.

But then on the other side there is even a test like this

it('does not throw when even one key does not exist', () => {
  expect(() => {
    expect({ a: 1, c: 3 }).toExcludeKeys([ 'a', 'b', 'c' ])
  }).toNotThrow()
})

That looks more like, there needs to be at least on key in keys which is not in object and then it’s fine.

Thanks.

About this issue

Most upvoted comments

expect([a: 0, c: 0]).toExcludeAnyKeys(['a', 'b', 'c'])

Could be read as:

Expect [ a: 0, c: 0 ] to be missing at least one of the following keys: [ ‘a’, ‘b’, ‘c’ ]

This should pass, because [ a: 0, b: 0 ] excludes the key b.


expect([a: 0, c: 0]).toExcludeAllKeys(['a', 'b', 'c'])

Could be read as:

Expect [ a: 0, c: 0 ] to be missing all of the following keys: [ ‘a’, ‘b’, ‘c’ ]

Should throw (not pass), because [ a: 0, c: 0 ] only excludes b, but does not exclude a or c

@ljharb For sure 😃 I was just trying to sort out how we’re defining toExcludeAnyKeys and toExcludeAllKeys

I do see the value in having both toExcludeAnyKeys and toExcludeAllKeys. Perhaps it’s worth adding the “any” version?