Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show diff correctly #3620

Merged
merged 2 commits into from Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/utils/src/diff/index.ts
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
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
@@ -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
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