[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(utils): fix asymmetric matcher diff inside array (#5189)

authored by

Hiroshi Ogawa and committed by
GitHub
(Feb 13, 2024, 8:27 AM +0100) 3ffcd2ea 7d18233c

+67 -1
+1 -1
packages/utils/src/error.ts
··· 141 141 function isReplaceable(obj1: any, obj2: any) { 142 142 const obj1Type = getType(obj1) 143 143 const obj2Type = getType(obj2) 144 - return obj1Type === obj2Type && obj1Type === 'Object' 144 + return obj1Type === obj2Type && (obj1Type === 'Object' || obj1Type === 'Array') 145 145 } 146 146 147 147 export function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced = new WeakSet(), expectedReplaced = new WeakSet()) {
+66
test/core/test/diff.test.ts
··· 1 1 import { expect, test, vi } from 'vitest' 2 2 import { getDefaultColors, setupColors } from '@vitest/utils' 3 3 import { diff } from '@vitest/utils/diff' 4 + import { processError } from '@vitest/runner' 4 5 import { displayDiff } from '../../../packages/vitest/src/node/error' 5 6 6 7 test('displays object diff', () => { ··· 59 60 " 60 61 `) 61 62 }) 63 + 64 + test('asymmetric matcher in object', () => { 65 + setupColors(getDefaultColors()) 66 + expect(getErrorDiff({ x: 0, y: 'foo' }, { x: 1, y: expect.anything() })).toMatchInlineSnapshot(` 67 + "- Expected 68 + + Received 69 + 70 + Object { 71 + - "x": 1, 72 + + "x": 0, 73 + "y": Anything, 74 + }" 75 + `) 76 + }) 77 + 78 + test('asymmetric matcher in array', () => { 79 + setupColors(getDefaultColors()) 80 + expect(getErrorDiff([0, 'foo'], [1, expect.anything()])).toMatchInlineSnapshot(` 81 + "- Expected 82 + + Received 83 + 84 + Array [ 85 + - 1, 86 + + 0, 87 + Anything, 88 + ]" 89 + `) 90 + }) 91 + 92 + test('asymmetric matcher in nested', () => { 93 + setupColors(getDefaultColors()) 94 + expect( 95 + getErrorDiff( 96 + [{ x: 0, y: 'foo' }, [0, 'bar']], 97 + [{ x: 1, y: expect.anything() }, [1, expect.anything()]], 98 + ), 99 + ).toMatchInlineSnapshot(` 100 + "- Expected 101 + + Received 102 + 103 + Array [ 104 + Object { 105 + - "x": 1, 106 + + "x": 0, 107 + "y": Anything, 108 + }, 109 + Array [ 110 + - 1, 111 + + 0, 112 + Anything, 113 + ], 114 + ]" 115 + `) 116 + }) 117 + 118 + function getErrorDiff(actual: unknown, expected: unknown) { 119 + try { 120 + expect(actual).toEqual(expected) 121 + } 122 + catch (e) { 123 + const error = processError(e) 124 + return error.diff 125 + } 126 + expect.unreachable() 127 + }