Skip to content

Add failing element to satisfyAll and satisfyAny error messages #482

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 9 additions & 4 deletions src/matchers/toSatisfyAll.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
export function toSatisfyAll(actual, expected) {
const { printReceived, printExpected, matcherHint } = this.utils;

const pass = actual.every(expected);
const failingElement = pass ? actual.find(expected) : actual.find(a => !expected(a));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be multiple failing elements, right? I'm wondering if it'd be better to print them all instead of just the first one.


const passMessage =
matcherHint('.not.toSatisfyAll') +
'\n\n' +
'Expected array to not satisfy predicate for all values:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;
` ${printReceived(actual)}\n` +
'Failed on:\n' +
` ${printReceived(failingElement)}`;

const failMessage =
matcherHint('.toSatisfyAll') +
'\n\n' +
'Expected array to satisfy predicate for all values:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const pass = actual.every(expected);
` ${printReceived(actual)}\n` +
'Failed on:\n' +
` ${printReceived(failingElement)}`;

return { pass, message: () => (pass ? passMessage : failMessage) };
}
13 changes: 9 additions & 4 deletions src/matchers/toSatisfyAny.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
export function toSatisfyAny(actual, expected) {
const { printReceived, printExpected, matcherHint } = this.utils;

const pass = actual.some(expected);
const failingElement = pass ? actual.find(expected) : actual.find(a => !expected(a));
Copy link
Collaborator

@keeganwitt keeganwitt Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return the first non-matching element? In the case of toSatisfyAny, all elements would fail the predicate during failure scenarios.


const passMessage =
matcherHint('.not.toSatisfyAny') +
'\n\n' +
'Expected array to not satisfy predicate for any value:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;
` ${printReceived(actual)}\n` +
'Failed on:\n' +
` ${printReceived(failingElement)}`;

const failMessage =
matcherHint('.toSatisfyAny') +
'\n\n' +
'Expected array to satisfy predicate for any values:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const pass = actual.some(expected);
` ${printReceived(actual)}\n` +
'Failed on:\n' +
` ${printReceived(failingElement)}`;

return { pass, message: () => (pass ? passMessage : failMessage) };
}
8 changes: 6 additions & 2 deletions test/matchers/__snapshots__/toSatisfyAll.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ exports[`.not.toSatisfyAll fails when all values satisfy predicate 1`] = `
Expected array to not satisfy predicate for all values:
<green>[Function isOdd]</color>
Received:
<red>[1, 3, 5, 7]</color>"
<red>[1, 3, 5, 7]</color>
Failed on:
<red>1</color>"
`;

exports[`.toSatisfyAll fails when any value does not satisfy the predicate 1`] = `
Expand All @@ -15,5 +17,7 @@ exports[`.toSatisfyAll fails when any value does not satisfy the predicate 1`] =
Expected array to satisfy predicate for all values:
<green>[Function isOdd]</color>
Received:
<red>[1, 3, 4, 5]</color>"
<red>[1, 3, 4, 5]</color>
Failed on:
<red>4</color>"
`;
8 changes: 6 additions & 2 deletions test/matchers/__snapshots__/toSatisfyAny.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ exports[`.not.toSatisfyAll fails when any value satisfies predicate 1`] = `
Expected array to not satisfy predicate for any value:
<green>[Function isOdd]</color>
Received:
<red>[2, 3, 6, 8]</color>"
<red>[2, 3, 6, 8]</color>
Failed on:
<red>3</color>"
`;

exports[`.toSatisfyAny fails when no value satisfies the predicate 1`] = `
Expand All @@ -15,5 +17,7 @@ exports[`.toSatisfyAny fails when no value satisfies the predicate 1`] = `
Expected array to satisfy predicate for any values:
<green>[Function isOdd]</color>
Received:
<red>[2, 4, 6, 8]</color>"
<red>[2, 4, 6, 8]</color>
Failed on:
<red>2</color>"
`;