Skip to content

Commit f8f70f7

Browse files
authoredDec 19, 2023
fix(expect): fix toHaveProperty assertion error diff (#4734)
1 parent f859efc commit f8f70f7

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed
 

‎packages/expect/src/jest-expect.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
330330
if (Array.isArray(args[0]))
331331
args[0] = args[0].map(key => String(key).replace(/([.[\]])/g, '\\$1')).join('.')
332332

333-
const actual = this._obj
333+
const actual = this._obj as any
334334
const [propertyName, expected] = args
335335
const getValue = () => {
336336
const hasOwn = Object.prototype.hasOwnProperty.call(actual, propertyName)
@@ -347,7 +347,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
347347
pass,
348348
`expected #{this} to have property "${propertyName}"${valueString}`,
349349
`expected #{this} to not have property "${propertyName}"${valueString}`,
350-
actual,
350+
expected,
351+
exists ? value : undefined,
351352
)
352353
})
353354
def('toBeCloseTo', function (received: number, precision = 2) {

‎test/core/test/jest-expect.test.ts

+92
Original file line numberDiff line numberDiff line change
@@ -903,4 +903,96 @@ it('correctly prints diff with asymmetric matchers', () => {
903903
}
904904
})
905905

906+
it('toHaveProperty error diff', () => {
907+
setupColors(getDefaultColors())
908+
909+
// make it easy for dev who trims trailing whitespace on IDE
910+
function trim(s: string): string {
911+
return s.replaceAll(/ *$/gm, '')
912+
}
913+
914+
function getError(f: () => unknown) {
915+
try {
916+
f()
917+
return expect.unreachable()
918+
}
919+
catch (error) {
920+
const processed = processError(error)
921+
return [processed.message, trim(processed.diff)]
922+
}
923+
}
924+
925+
// non match value
926+
expect(getError(() => expect({ name: 'foo' }).toHaveProperty('name', 'bar'))).toMatchInlineSnapshot(`
927+
[
928+
"expected { name: 'foo' } to have property "name" with value 'bar'",
929+
"- Expected
930+
+ Received
931+
932+
- bar
933+
+ foo",
934+
]
935+
`)
936+
937+
// non match key
938+
expect(getError(() => expect({ noName: 'foo' }).toHaveProperty('name', 'bar'))).toMatchInlineSnapshot(`
939+
[
940+
"expected { noName: 'foo' } to have property "name" with value 'bar'",
941+
"- Expected:
942+
"bar"
943+
944+
+ Received:
945+
undefined",
946+
]
947+
`)
948+
949+
// non match value (with asymmetric matcher)
950+
expect(getError(() => expect({ name: 'foo' }).toHaveProperty('name', expect.any(Number)))).toMatchInlineSnapshot(`
951+
[
952+
"expected { name: 'foo' } to have property "name" with value Any{ …(3) }",
953+
"- Expected:
954+
Any<Number>
955+
956+
+ Received:
957+
"foo"",
958+
]
959+
`)
960+
961+
// non match key (with asymmetric matcher)
962+
expect(getError(() => expect({ noName: 'foo' }).toHaveProperty('name', expect.any(Number)))).toMatchInlineSnapshot(`
963+
[
964+
"expected { noName: 'foo' } to have property "name" with value Any{ …(3) }",
965+
"- Expected:
966+
Any<Number>
967+
968+
+ Received:
969+
undefined",
970+
]
971+
`)
972+
973+
// non match value (deep key)
974+
expect(getError(() => expect({ parent: { name: 'foo' } }).toHaveProperty('parent.name', 'bar'))).toMatchInlineSnapshot(`
975+
[
976+
"expected { parent: { name: 'foo' } } to have property "parent.name" with value 'bar'",
977+
"- Expected
978+
+ Received
979+
980+
- bar
981+
+ foo",
982+
]
983+
`)
984+
985+
// non match key (deep key)
986+
expect(getError(() => expect({ parent: { noName: 'foo' } }).toHaveProperty('parent.name', 'bar'))).toMatchInlineSnapshot(`
987+
[
988+
"expected { parent: { noName: 'foo' } } to have property "parent.name" with value 'bar'",
989+
"- Expected:
990+
"bar"
991+
992+
+ Received:
993+
undefined",
994+
]
995+
`)
996+
})
997+
906998
it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))

0 commit comments

Comments
 (0)
Please sign in to comment.