| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,12 @@ const FALLBACK_FORMAT_OPTIONS = { | |||
| 60 | 60 | // Generate a string that will highlight the difference between two values | |
| 61 | 61 | // with green and red. (similar to how github does code diffing) | |
| 62 | 62 | ||
| 63 | + /** | ||
| 64 | + * @param a Expected value | ||
| 65 | + * @param b Received value | ||
| 66 | + * @param options Diff options | ||
| 67 | + * @returns | ||
| 68 | + */ | ||
| 63 | 69 | export function diff(a: any, b: any, options?: DiffOptions): string | null { | |
| 64 | 70 | if (Object.is(a, b)) | |
| 65 | 71 | return '' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,7 +97,7 @@ export function processError(err: any) { | |||
| 97 | 97 | err.nameStr = String(err.name) | |
| 98 | 98 | ||
| 99 | 99 | if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined)) | |
| 100 | - err.diff = diff(err.actual, err.expected) | ||
| 100 | + err.diff = diff(err.expected, err.actual) | ||
| 101 | 101 | ||
| 102 | 102 | if (typeof err.expected !== 'string') | |
| 103 | 103 | err.expected = stringify(err.expected, 10) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,9 @@ | |||
| 1 | 1 | /* eslint-disable no-sparse-arrays */ | |
| 2 | 2 | import { AssertionError } from 'node:assert' | |
| 3 | 3 | import { describe, expect, it, vi } from 'vitest' | |
| 4 | - import { generateToBeMessage } from '@vitest/expect' | ||
| 4 | + import { generateToBeMessage, setupColors } from '@vitest/expect' | ||
| 5 | + import { processError } from '@vitest/utils/error' | ||
| 6 | + import { getDefaultColors } from '@vitest/utils' | ||
| 5 | 7 | ||
| 6 | 8 | class TestError extends Error {} | |
| 7 | 9 | ||
@@ -753,4 +755,17 @@ it('compatible with jest', () => { | |||
| 753 | 755 | expect(state.assertionCalls).toBe(2) | |
| 754 | 756 | }) | |
| 755 | 757 | ||
| 758 | + it('correctly prints diff', () => { | ||
| 759 | + try { | ||
| 760 | + expect({ a: 1 }).toEqual({ a: 2 }) | ||
| 761 | + expect.unreachable() | ||
| 762 | + } | ||
| 763 | + catch (err) { | ||
| 764 | + setupColors(getDefaultColors()) | ||
| 765 | + const error = processError(err) | ||
| 766 | + expect(error.diff).toContain('- "a": 2') | ||
| 767 | + expect(error.diff).toContain('+ "a": 1') | ||
| 768 | + } | ||
| 769 | + }) | ||
| 770 | + | ||
| 756 | 771 | it('timeout', () => new Promise(resolve => setTimeout(resolve, 500))) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,8 +48,8 @@ exports[`html reporter > resolves to "failing" status for test file "json-fail" | |||
| 48 | 48 | "diff": "- Expected | |
| 49 | 49 | + Received | |
| 50 | 50 | ||
| 51 | - - 2 | ||
| 52 | - + 1", | ||
| 51 | + - 1 | ||
| 52 | + + 2", | ||
| 53 | 53 | "expected": "1", | |
| 54 | 54 | "message": "expected 2 to deeply equal 1", | |
| 55 | 55 | "name": "AssertionError", | |
@@ -68,8 +68,8 @@ exports[`html reporter > resolves to "failing" status for test file "json-fail" | |||
| 68 | 68 | "diff": "- Expected | |
| 69 | 69 | + Received | |
| 70 | 70 | ||
| 71 | - - 2 | ||
| 72 | - + 1", | ||
| 71 | + - 1 | ||
| 72 | + + 2", | ||
| 73 | 73 | "expected": "1", | |
| 74 | 74 | "message": "expected 2 to deeply equal 1", | |
| 75 | 75 | "name": "AssertionError", | |
| Back | FazBrowse Home | New Git URL |
0 commit comments