Skip to content

assert.hasAllKeys incorrectly assumes expected keys do not contain duplicates #1705

@madx

Description

@madx

While writing a test using assert.hasAllKeys, I encountered an issue when the array of expected keys contains a duplicate. The following code demonstrates the bug:

test('hasAllKeys', () => {
  assert.hasAllKeys({ a: 1, b: 2 }, ['a', 'a'])
})
// => test passes

I expect this test to fail because of the presence of b, but it actually passes when running the test suite. It might seem like having a duplicate is unlikely, but that is something that can happen when you quickly want to swap a value to make sure that the test fails with the wrong set of keys. I also think that the current semantics are a bit misleading when compared to the documentation of the hasAllKeys function.

I think I found the incriminated code and am willing to submit a PR to fix the issue if that is something that you would consider. The second part of the block should instead check that every key in actual is also present in expected. This will add a small computation cost as it will require another double loop. Another way to do it would be to keep only unique values in expected.

Environment: ArchLinux, vitest 3.2.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions