Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-keys-recover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jest-extended': patch
---

Fix `toContainAllKeys` throwing a `TypeError` instead of producing an assertion failure message when the received value is `null` or `undefined`
6 changes: 4 additions & 2 deletions src/matchers/toContainAllKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function toContainAllKeys<E = unknown>(actual: unknown, expected: readonl
expected.every(key => contains((a, b) => this.equals(a, b, this.customTesters), objectKeys, key));
}

const received = actual == null ? actual : Object.keys(actual as Record<string, unknown>);

return {
pass,
message: () =>
Expand All @@ -22,12 +24,12 @@ export function toContainAllKeys<E = unknown>(actual: unknown, expected: readonl
'Expected object to not contain all keys:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(Object.keys(actual as Record<string, unknown>))}`
` ${printReceived(received)}`
: matcherHint('.toContainAllKeys') +
'\n\n' +
'Expected object to contain all keys:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(Object.keys(actual as Record<string, unknown>))}`,
` ${printReceived(received)}`,
};
}
18 changes: 17 additions & 1 deletion test/matchers/__snapshots__/toContainAllKeys.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ Received:
<red>["a", "b"]</color>"
`;

exports[`.toContainAllKeys fails when actual is not an object 1`] = `"Cannot convert undefined or null to object"`;
exports[`.toContainAllKeys fails when actual is not an object 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toContainAllKeys(</intensity><green>expected</color><dim>)</intensity>

Expected object to contain all keys:
<green>["a", "b"]</color>
Received:
<red>null</color>"
`;

exports[`.toContainAllKeys fails when actual is not an object 2`] = `
"<dim>expect(</intensity><red>received</color><dim>).toContainAllKeys(</intensity><green>expected</color><dim>)</intensity>

Expected object to contain all keys:
<green>["a", "b"]</color>
Received:
<red>undefined</color>"
`;

exports[`.toContainAllKeys fails when actual is not an object 3`] = `
"<dim>expect(</intensity><red>received</color><dim>).toContainAllKeys(</intensity><green>expected</color><dim>)</intensity>

Expected object to contain all keys:
<green>["a", "b"]</color>
Received:
Expand Down
1 change: 1 addition & 0 deletions test/matchers/toContainAllKeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('.toContainAllKeys', () => {

test('fails when actual is not an object', () => {
expect(() => expect(null).toContainAllKeys(['a', 'b'])).toThrowErrorMatchingSnapshot();
expect(() => expect(undefined).toContainAllKeys(['a', 'b'])).toThrowErrorMatchingSnapshot();
expect(() => expect(42).toContainAllKeys(['a', 'b'])).toThrowErrorMatchingSnapshot();
});
});
Expand Down