From de4464cb9a13cb8d0263d094840633898cd3af72 Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Tue, 17 Aug 2021 16:56:21 -0400 Subject: [PATCH] fix(expect): highlight diff in toHaveText (#8262) --- src/test/matchers/toMatchText.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/test/matchers/toMatchText.ts b/src/test/matchers/toMatchText.ts index 8f1ddae651..15b0169d57 100644 --- a/src/test/matchers/toMatchText.ts +++ b/src/test/matchers/toMatchText.ts @@ -21,12 +21,11 @@ import { import { EXPECTED_COLOR, - getLabelPrinter, matcherErrorMessage, matcherHint, MatcherHintOptions, printExpected, - printReceived, printWithType, + printDiffOrStringify, } from 'jest-matcher-utils'; import { currentTestInfo } from '../globals'; import type { Expect } from '../types'; @@ -107,14 +106,17 @@ export async function toMatchText( const labelExpected = `Expected ${typeof expected === 'string' ? stringSubstring : 'pattern' }`; const labelReceived = 'Received string'; - const printLabel = getLabelPrinter(labelExpected, labelReceived); return ( matcherHint(matcherName, undefined, undefined, matcherOptions) + '\n\n' + - `${printLabel(labelExpected)}${printExpected(expected)}\n` + - `${printLabel(labelReceived)}${printReceived(received)}` - ); + printDiffOrStringify( + expected, + received, + labelExpected, + labelReceived, + this.expand !== false, + )); }; return { message, pass };