[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(expect): fix `toEqual` and `toMatchObject` with circular references (#5535)

authored by

Hiroshi Ogawa and committed by
GitHub
(Apr 17, 2024, 2:47 PM +0200) 9e6417c9 ea3c16e4

+73 -12
+11 -10
packages/expect/src/jest-expect.ts
··· 164 164 const pass = jestEquals(actual, expected, [...customTesters, iterableEquality, subsetEquality]) 165 165 const isNot = utils.flag(this, 'negate') as boolean 166 166 const { subset: actualSubset, stripped } = getObjectSubset(actual, expected) 167 - const msg = utils.getMessage( 168 - this, 169 - [ 170 - pass, 171 - 'expected #{this} to match object #{exp}', 172 - 'expected #{this} to not match object #{exp}', 173 - expected, 174 - actualSubset, 175 - ], 176 - ) 177 167 if ((pass && isNot) || (!pass && !isNot)) { 168 + const msg = utils.getMessage( 169 + this, 170 + [ 171 + pass, 172 + 'expected #{this} to match object #{exp}', 173 + 'expected #{this} to not match object #{exp}', 174 + expected, 175 + actualSubset, 176 + false, 177 + ], 178 + ) 178 179 const message = stripped === 0 ? msg : `${msg}\n(${stripped} matching ${stripped === 1 ? 'property' : 'properties'} omitted from actual)` 179 180 throw new AssertionError(message, { showDiff: true, expected, actual: actualSubset }) 180 181 }
+2 -2
packages/expect/src/jest-utils.ts
··· 334 334 return iterableEquality( 335 335 a, 336 336 b, 337 - [...filteredCustomTesters], 337 + [...customTesters], 338 338 [...aStack], 339 339 [...bStack], 340 340 ) ··· 452 452 return undefined 453 453 454 454 return Object.keys(subset).every((key) => { 455 - if (isObjectWithKeys(subset[key])) { 455 + if (typeof subset[key] === 'object') { 456 456 if (seenReferences.has(subset[key])) 457 457 return equals(object[key], subset[key], filteredCustomTesters) 458 458
+60
test/core/test/expect-circular.test.ts
··· 1 + import { describe, expect, test } from 'vitest' 2 + 3 + describe('circular equality', () => { 4 + test('object, set, map', () => { 5 + // https://github.com/vitest-dev/vitest/issues/5533 6 + function gen() { 7 + const obj = { 8 + a: new Set<any>(), 9 + b: new Map<any, any>(), 10 + } 11 + obj.a.add(obj) 12 + obj.b.set('k', obj) 13 + return obj 14 + } 15 + expect(gen()).toEqual(gen()) 16 + expect(gen()).toMatchObject(gen()) 17 + }) 18 + 19 + test('object, set', () => { 20 + function gen() { 21 + const obj = { 22 + a: new Set<any>(), 23 + b: new Set<any>(), 24 + } 25 + obj.a.add(obj) 26 + obj.b.add(obj) 27 + return obj 28 + } 29 + expect(gen()).toEqual(gen()) 30 + expect(gen()).toMatchObject(gen()) 31 + }) 32 + 33 + test('array, set', () => { 34 + function gen() { 35 + const obj = [new Set<any>(), new Set<any>()] 36 + obj[0].add(obj) 37 + obj[1].add(obj) 38 + return obj 39 + } 40 + expect(gen()).toEqual(gen()) 41 + expect(gen()).toMatchObject(gen()) 42 + }) 43 + 44 + test('object, array', () => { 45 + // https://github.com/jestjs/jest/issues/14734 46 + function gen() { 47 + const a: any = { 48 + v: 1, 49 + } 50 + const c1: any = { 51 + ref: [], 52 + } 53 + c1.ref.push(c1) 54 + a.ref = c1 55 + return a 56 + } 57 + expect(gen()).toEqual(gen()) 58 + expect(gen()).toMatchObject(gen()) 59 + }) 60 + })