|
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,3 +60,68 @@ test('display multiline line string 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 | +} |
0 commit comments