diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 3209199545ba..1df2260926e8 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -209,6 +209,16 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { actual.value, ) } + // handle simple case on our own using `this.assert` to include diff in error message + if (typeof actual === 'string' && typeof item === 'string') { + return this.assert( + actual.includes(item), + `expected #{this} to contain #{exp}`, + `expected #{this} not to contain #{exp}`, + item, + actual, + ) + } // make "actual" indexable to have compatibility with jest if (actual != null && typeof actual !== 'string') utils.flag(this, 'object', Array.from(actual as Iterable)) diff --git a/test/core/test/__snapshots__/jest-expect.test.ts.snap b/test/core/test/__snapshots__/jest-expect.test.ts.snap index 71415a56e167..d1f7b3d9b6de 100644 --- a/test/core/test/__snapshots__/jest-expect.test.ts.snap +++ b/test/core/test/__snapshots__/jest-expect.test.ts.snap @@ -320,11 +320,24 @@ exports[`toMatch/toContain diff 1`] = ` - world + hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "expected": "world", - "message": "expected 'hellohellohellohellohellohellohellohe…' to match 'world'", + "message": "expected 'hellohellohellohellohellohellohellohe…' to contain 'world'", } `; exports[`toMatch/toContain diff 2`] = ` +{ + "actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", + "diff": "- Expected ++ Received + +- world ++ hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", + "expected": "world", + "message": "expected 'hellohellohellohellohellohellohellohe…' to match 'world'", +} +`; + +exports[`toMatch/toContain diff 3`] = ` { "actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "diff": "- Expected: diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 7ff3009e1f6f..990361d41146 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1086,6 +1086,7 @@ it('asymmetric matcher error', () => { }) it('toMatch/toContain diff', () => { + snapshotError(() => expect('hello'.repeat(20)).toContain('world')) snapshotError(() => expect('hello'.repeat(20)).toMatch('world')) snapshotError(() => expect('hello'.repeat(20)).toMatch(/world/)) })