Skip to content

Commit

Permalink
fix: show diff correctly (#3620)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 19, 2023
1 parent 4caae81 commit 73dd4ab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const FALLBACK_FORMAT_OPTIONS = {
// Generate a string that will highlight the difference between two values
// with green and red. (similar to how github does code diffing)

/**
* @param a Expected value
* @param b Received value
* @param options Diff options
* @returns
*/
export function diff(a: any, b: any, options?: DiffOptions): string | null {
if (Object.is(a, b))
return ''
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function processError(err: any) {
err.nameStr = String(err.name)

if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined))
err.diff = diff(err.actual, err.expected)
err.diff = diff(err.expected, err.actual)

if (typeof err.expected !== 'string')
err.expected = stringify(err.expected, 10)
Expand Down
17 changes: 16 additions & 1 deletion test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-sparse-arrays */
import { AssertionError } from 'node:assert'
import { describe, expect, it, vi } from 'vitest'
import { generateToBeMessage } from '@vitest/expect'
import { generateToBeMessage, setupColors } from '@vitest/expect'
import { processError } from '@vitest/utils/error'
import { getDefaultColors } from '@vitest/utils'

class TestError extends Error {}

Expand Down Expand Up @@ -753,4 +755,17 @@ it('compatible with jest', () => {
expect(state.assertionCalls).toBe(2)
})

it('correctly prints diff', () => {
try {
expect({ a: 1 }).toEqual({ a: 2 })
expect.unreachable()
}
catch (err) {
setupColors(getDefaultColors())
const error = processError(err)
expect(error.diff).toContain('- "a": 2')
expect(error.diff).toContain('+ "a": 1')
}
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))
8 changes: 4 additions & 4 deletions test/reporters/tests/__snapshots__/html.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ exports[`html reporter > resolves to "failing" status for test file "json-fail"
"diff": "- Expected
+ Received
- 2
+ 1",
- 1
+ 2",
"expected": "1",
"message": "expected 2 to deeply equal 1",
"name": "AssertionError",
Expand All @@ -68,8 +68,8 @@ exports[`html reporter > resolves to "failing" status for test file "json-fail"
"diff": "- Expected
+ Received
- 2
+ 1",
- 1
+ 2",
"expected": "1",
"message": "expected 2 to deeply equal 1",
"name": "AssertionError",
Expand Down

0 comments on commit 73dd4ab

Please sign in to comment.