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

toPartiallyContain matcher throws exception if array contains null/undefined #579

Open
MikeWinter opened this issue Mar 14, 2023 · 0 comments

Comments

@MikeWinter
Copy link

Bug

  • package version: 3.2.3
  • node version: 16.19.0
  • npm version: 8.19.3

Relevant code or config:

it("matches the desired object", () => {
  const actual = [null, { key: "value" }];

  expect(actual).toPartiallyContain({ key: "value" });
});

What you did:

By a fluke of data generation, some of the elements in an array returned in our code could contain null. These values would be filtered out later, but they were nevertheless produced by the function under test.

The expectation was that these null values would be skipped by the toPartiallyContain matcher as non-matches, but it would still find candidate objects. That is, I'd expect the test above to pass because the array does contain a matching object as one of the later elements.

What happened (please provide anything you think will help):

The matcher throws an exception when attempting to access the hasOwnProperty member of the null element:

Cannot read properties of null (reading 'hasOwnProperty')

A similar exception is also raised if an element is undefined.

The origin of the exception is in the containsEntry utility function when evaluating the first expression. As such:

  • it affects the toIncludeAllPartialMembers matcher if, like the earlier example, the "actual" array contains a null-ish value
  • the toContainEntry, toContainEntries, and toContainAllEntries matchers are affected if the "actual" object is itself null-ish

Reproduction repository (if possible):

Unfortunately it isn't public, but I could prepare something if the example above isn't sufficient.

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

1 participant