jest-community/jest-extended

toPartiallyContain matcher throws exception if array contains null/undefined

MikeWinter opened this issue · 0 comments

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.