|
| 1 | +import { expect, test, vi } from 'vitest' |
| 2 | +import { displayDiff } from 'vitest/src/node/error' |
| 3 | +import { stringify } from 'vitest/src/integrations/chai/jest-matcher-utils' |
| 4 | + |
| 5 | +test('displays an error for large objects', () => { |
| 6 | + const objectA = new Array(1000).fill(0).map((_, i) => ({ i, long: 'a'.repeat(i) })) |
| 7 | + const objectB = new Array(1000).fill(0).map((_, i) => ({ i, long: 'b'.repeat(i) })) |
| 8 | + const console = { log: vi.fn(), error: vi.fn() } |
| 9 | + displayDiff(stringify(objectA), stringify(objectB), console as any, { noColor: true }) |
| 10 | + expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(` |
| 11 | + "Could not display diff. It's possible objects are too large to compare. |
| 12 | + Try increasing --outputDiffMaxSize option. |
| 13 | + " |
| 14 | + `) |
| 15 | +}) |
| 16 | + |
| 17 | +test('displays an error for large objects', () => { |
| 18 | + const console = { log: vi.fn(), error: vi.fn() } |
| 19 | + displayDiff(stringify('undefined'), stringify('undefined'), console as any, { noColor: true }) |
| 20 | + expect(console.error).not.toHaveBeenCalled() |
| 21 | +}) |
| 22 | + |
| 23 | +test('displays diff', () => { |
| 24 | + const objectA = { a: 1, b: 2 } |
| 25 | + const objectB = { a: 1, b: 3 } |
| 26 | + const console = { log: vi.fn(), error: vi.fn() } |
| 27 | + displayDiff(stringify(objectA), stringify(objectB), console as any, { noColor: true }) |
| 28 | + expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(` |
| 29 | + " - Expected - 1 |
| 30 | + + Received + 1 |
| 31 | +
|
| 32 | + Object { |
| 33 | + \\"a\\": 1, |
| 34 | + - \\"b\\": 3, |
| 35 | + + \\"b\\": 2, |
| 36 | + } |
| 37 | + " |
| 38 | + `) |
| 39 | +}) |
| 40 | + |
| 41 | +test('displays long diff', () => { |
| 42 | + const objectA = { a: 1, b: 2, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 } |
| 43 | + const objectB = { a: 1, b: 3, k: 11, l: 12, m: 13, n: 14, p: 16, o: 17, r: 18, s: 23, t: 88, u: 21, v: 44, w: 23, x: 24, y: 25, z: 26 } |
| 44 | + const console = { log: vi.fn(), error: vi.fn() } |
| 45 | + displayDiff(stringify(objectA), stringify(objectB), console as any, { noColor: true, outputDiffMaxLines: 5 }) |
| 46 | + expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(` |
| 47 | + " - Expected - 5 |
| 48 | + + Received + 13 |
| 49 | +
|
| 50 | + Object { |
| 51 | + \\"a\\": 1, |
| 52 | + - \\"b\\": 3, |
| 53 | + + \\"b\\": 2, |
| 54 | + + \\"d\\": 4, |
| 55 | + ... 26 more lines |
| 56 | + " |
| 57 | + `) |
| 58 | +}) |
| 59 | + |
| 60 | +test('displays truncated diff', () => { |
| 61 | + const stringA = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 62 | +Suspendisse viverra sapien ac venenatis lacinia. |
| 63 | +Morbi consectetur arcu nec lorem lacinia tempus.` |
| 64 | + const objectB = `Quisque hendrerit metus id dapibus pulvinar. |
| 65 | +Quisque pellentesque enim a elit faucibus cursus. |
| 66 | +Sed in tellus aliquet mauris interdum semper a in lacus.` |
| 67 | + const console = { log: vi.fn(), error: vi.fn() } |
| 68 | + displayDiff((stringA), (objectB), console as any, { noColor: true, outputTruncateLength: 14 }) |
| 69 | + expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(` |
| 70 | + " - Expected - 3 |
| 71 | + + Received + 3 |
| 72 | +
|
| 73 | + - Quisque h… |
| 74 | + - Quisque p… |
| 75 | + - Sed in te… |
| 76 | + + Lorem ips… |
| 77 | + + Suspendis… |
| 78 | + + Morbi con… |
| 79 | + " |
| 80 | + `) |
| 81 | +}) |
0 commit comments