diff --git a/src/matchers/toIncludeSameMembers.js b/src/matchers/toIncludeSameMembers.js index 9d87f6fe..7412d1b0 100644 --- a/src/matchers/toIncludeSameMembers.js +++ b/src/matchers/toIncludeSameMembers.js @@ -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, @@ -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; } else { diff --git a/test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap b/test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap index 45140afe..97958ca9 100644 --- a/test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap +++ b/test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap @@ -139,10 +139,17 @@ Received: exports[`.toIncludeSameMembers have gaps simple items 1`] = ` "expect(received).toIncludeSameMembers(expected) -Expected list to have the following members and no more: - [1, 2, 3, 4, 5] -Received: - [5, 6, 1]" +- Expected - 3 ++ Received + 1 + + Array [ + 1, +- 2, +- 3, +- 4, ++ 6, + 5, + ]" `; exports[`.toIncludeSameMembers keyOrFn passed function 1`] = `