Skip to content

fix(matchers): avoid TypeError in toContainAllKeys when received is nullish#935

Open
greymoth-jp wants to merge 1 commit into
jest-community:mainfrom
greymoth-jp:fix/to-contain-all-keys-nullish
Open

fix(matchers): avoid TypeError in toContainAllKeys when received is nullish#935
greymoth-jp wants to merge 1 commit into
jest-community:mainfrom
greymoth-jp:fix/to-contain-all-keys-nullish

Conversation

@greymoth-jp

Copy link
Copy Markdown

toContainAllKeys builds its failure message from Object.keys(actual), called unconditionally in both branches. When the received value is null or undefined this throws TypeError: Cannot convert undefined or null to object, so you get an unrelated crash instead of a normal assertion failure:

expect(null).toContainAllKeys(['a', 'b']);
// TypeError: Cannot convert undefined or null to object

The pass result itself is already null-safe (it is guarded by typeof actual === 'object' && actual !== null); only the message builder is affected.

The sibling key matchers toContainKey, toContainKeys and toContainAnyKeys were made null-safe in #878 and print the received value directly. This does the same for toContainAllKeys: the message prints the raw value for null/undefined (matching toContainKey's output) and is unchanged for every other input.

The existing test already called expect(null).toContainAllKeys(['a', 'b']) under "fails when actual is not an object", but its snapshot had captured the TypeError text rather than an assertion message. The snapshot is updated and an undefined case is added to match the sibling tests.

…ullish

Object.keys(actual) was called unconditionally when building the failure message, throwing TypeError for null/undefined instead of a clean assertion message. Print the raw received value for nullish input, matching toContainKey (made null-safe in jest-community#878).
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cd042f2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jest-extended Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

Successfully merging this pull request may close these issues.

1 participant