Skip to content

Commit d939c1a

Browse files
committed
fix: preserve constructor in toMatchObject diff
1 parent be6abe8 commit d939c1a

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

packages/expect/src/jest-utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ export function getObjectSubset(
709709
const trimmed: any = {}
710710
seenReferences.set(object, trimmed)
711711

712+
// preserve constructor for toMatchObject diff
713+
if (typeof object.constructor === 'function' && typeof object.constructor.name === 'string') {
714+
Object.defineProperty(trimmed, 'constructor', {
715+
enumerable: false,
716+
value: object.constructor,
717+
})
718+
}
719+
712720
for (const key of getObjectKeys(object)) {
713721
if (hasPropertyInObject(subset, key)) {
714722
trimmed[key] = seenReferences.has(object[key])

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

+32-12
Original file line numberDiff line numberDiff line change
@@ -1062,11 +1062,11 @@ it('toMatchObject error diff', () => {
10621062

10631063
// https://github.com/vitest-dev/vitest/issues/6543
10641064
class Foo {
1065-
constructor(public value: number) {}
1065+
constructor(public value: any) {}
10661066
}
10671067

10681068
class Bar {
1069-
constructor(public value: number) {}
1069+
constructor(public value: any) {}
10701070
}
10711071

10721072
expect(new Foo(0)).toMatchObject(new Bar(0))
@@ -1081,7 +1081,7 @@ it('toMatchObject error diff', () => {
10811081
10821082
- Bar {
10831083
- "value": 1,
1084-
+ Object {
1084+
+ Foo {
10851085
+ "value": 0,
10861086
}",
10871087
]
@@ -1093,8 +1093,9 @@ it('toMatchObject error diff', () => {
10931093
"- Expected
10941094
+ Received
10951095
1096-
Object {
1096+
- Object {
10971097
- "value": 1,
1098+
+ Foo {
10981099
+ "value": 0,
10991100
}",
11001101
]
@@ -1116,30 +1117,49 @@ it('toMatchObject error diff', () => {
11161117

11171118
expect(getError(() =>
11181119
expect({
1119-
bad: new Bar(1),
1120-
good: new Bar(0),
1121-
}).toMatchObject({
1122-
bad: new Foo(2),
1120+
bad: new Foo(1),
11231121
good: new Foo(0),
1122+
}).toMatchObject({
1123+
bad: new Bar(2),
1124+
good: new Bar(0),
11241125
}),
11251126
)).toMatchInlineSnapshot(`
11261127
[
1127-
"expected { bad: Bar{ value: 1 }, …(1) } to match object { bad: Foo{ value: 2 }, …(1) }",
1128+
"expected { bad: Foo{ value: 1 }, …(1) } to match object { bad: Bar{ value: 2 }, …(1) }",
11281129
"- Expected
11291130
+ Received
11301131
11311132
Object {
1132-
- "bad": Foo {
1133+
- "bad": Bar {
11331134
- "value": 2,
1134-
+ "bad": Object {
1135+
+ "bad": Foo {
11351136
+ "value": 1,
11361137
},
1137-
"good": Foo {
1138+
"good": Bar {
11381139
"value": 0,
11391140
},
11401141
}",
11411142
]
11421143
`)
1144+
1145+
expect(getError(() =>
1146+
expect(new Foo(new Foo(1))).toMatchObject(new Bar(new Bar(0))),
1147+
)).toMatchInlineSnapshot(`
1148+
[
1149+
"expected Foo{ value: Foo{ value: 1 } } to match object Bar{ value: Bar{ value: +0 } }",
1150+
"- Expected
1151+
+ Received
1152+
1153+
- Bar {
1154+
- "value": Bar {
1155+
- "value": 0,
1156+
+ Foo {
1157+
+ "value": Foo {
1158+
+ "value": 1,
1159+
},
1160+
}",
1161+
]
1162+
`)
11431163
})
11441164

11451165
it('toHaveProperty error diff', () => {

0 commit comments

Comments
 (0)