Skip to content

Commit

Permalink
only fallback when have gaps and complex data type
Browse files Browse the repository at this point in the history
  • Loading branch information
rluvaton committed Aug 3, 2023
1 parent 3a34eb2 commit 5edc7e4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/matchers/toIncludeSameMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ const getBetterDiff = (equals, actual, expected, fnOrKey) => {

const pass = !invalid && added.length === 0 && missing.length === 0;

const containComplexDiffData = actual.concat(expected).some(item => typeof item === 'object' && item !== null);

// If we have gaps the output would be confusing and element will be displayed as removed and added for the wrong place when having partial match
if (invalid || !canFillTheGapsIfHave(newActual, added)) {
if (invalid || (containComplexDiffData && !canFillTheGapsIfHave(newActual, added))) {
return {
pass,
newActual: actual,
Expand Down Expand Up @@ -113,7 +115,7 @@ const getBetterDiff = (equals, actual, expected, fnOrKey) => {
let useDiffOutput;

// If Still have gaps fallback to the original array (the output would be confusing)
if (checkIfArrayHaveGaps && doesArrayHaveGaps(newActual)) {
if (checkIfArrayHaveGaps && containComplexDiffData && doesArrayHaveGaps(newActual)) {
newActual = actual;
useDiffOutput = false;

Check warning on line 120 in src/matchers/toIncludeSameMembers.js

View check run for this annotation

Codecov / codecov/patch

src/matchers/toIncludeSameMembers.js#L119-L120

Added lines #L119 - L120 were not covered by tests
} else {
Expand Down
15 changes: 11 additions & 4 deletions test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ Received:
exports[`.toIncludeSameMembers have gaps simple items 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeSameMembers(</intensity><green>expected</color><dim>)</intensity>
Expected list to have the following members and no more:
<green>[1, 2, 3, 4, 5]</color>
Received:
<red>[5, 6, 1]</color>"
<green>- Expected - 3</color>
<red>+ Received + 1</color>
<dim> Array [</intensity>
<dim> 1,</intensity>
<green>- 2,</color>
<green>- 3,</color>
<green>- 4,</color>
<red>+ 6,</color>
<dim> 5,</intensity>
<dim> ]</intensity>"
`;
exports[`.toIncludeSameMembers keyOrFn passed function 1`] = `
Expand Down

0 comments on commit 5edc7e4

Please sign in to comment.