@@ -2,15 +2,16 @@ import type { DiffOptions } from '@vitest/utils/diff'
2
2
import { stripVTControlCharacters } from 'node:util'
3
3
import { processError } from '@vitest/runner'
4
4
import { diff , diffStringsUnified , printDiffOrStringify } from '@vitest/utils/diff'
5
- import { expect , test , vi } from 'vitest'
6
- import { displayDiff } from '../../../packages/vitest/src/node/error'
5
+ import { expect , test } from 'vitest'
6
+
7
+ function wrapDiff ( diff ?: string ) {
8
+ return diff && stripVTControlCharacters ( `\n${ diff } \n` )
9
+ }
7
10
8
11
test ( 'displays string diff' , ( ) => {
9
12
const stringA = 'Hello AWorld'
10
13
const stringB = 'Hello BWorld'
11
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
12
- displayDiff ( printDiffOrStringify ( stringA , stringB ) , console as any )
13
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
14
+ expect ( wrapDiff ( printDiffOrStringify ( stringA , stringB ) ) ) . toMatchInlineSnapshot ( `
14
15
"
15
16
Expected: "Hello BWorld"
16
17
Received: "Hello AWorld"
@@ -21,9 +22,7 @@ test('displays string diff', () => {
21
22
test ( 'displays object diff' , ( ) => {
22
23
const objectA = { a : 1 , b : 2 }
23
24
const objectB = { a : 1 , b : 3 }
24
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
25
- displayDiff ( diff ( objectA , objectB ) , console as any )
26
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
25
+ expect ( wrapDiff ( diff ( objectA , objectB ) ) ) . toMatchInlineSnapshot ( `
27
26
"
28
27
- Expected
29
28
+ Received
@@ -40,9 +39,7 @@ test('displays object diff', () => {
40
39
test ( 'display truncated object diff' , ( ) => {
41
40
const objectA = { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 }
42
41
const objectB = { a : 1 , b : 3 , c : 4 , d : 5 , e : 6 }
43
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
44
- displayDiff ( diff ( objectA , objectB , { truncateThreshold : 4 } ) , console as any )
45
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
42
+ expect ( wrapDiff ( diff ( objectA , objectB , { truncateThreshold : 4 } ) ) ) . toMatchInlineSnapshot ( `
46
43
"
47
44
- Expected
48
45
+ Received
@@ -61,9 +58,7 @@ test('display truncated object diff', () => {
61
58
test ( 'display one line string diff' , ( ) => {
62
59
const string1 = 'string1'
63
60
const string2 = 'string2'
64
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
65
- displayDiff ( diff ( string1 , string2 ) , console as any )
66
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
61
+ expect ( wrapDiff ( diff ( string1 , string2 ) ) ) . toMatchInlineSnapshot ( `
67
62
"
68
63
- Expected
69
64
+ Received
@@ -77,9 +72,7 @@ test('display one line string diff', () => {
77
72
test ( 'display one line string diff should not be affected by truncateThreshold' , ( ) => {
78
73
const string1 = 'string1'
79
74
const string2 = 'string2'
80
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
81
- displayDiff ( diff ( string1 , string2 , { truncateThreshold : 3 } ) , console as any )
82
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
75
+ expect ( wrapDiff ( diff ( string1 , string2 , { truncateThreshold : 3 } ) ) ) . toMatchInlineSnapshot ( `
83
76
"
84
77
- Expected
85
78
+ Received
@@ -93,9 +86,7 @@ test('display one line string diff should not be affected by truncateThreshold',
93
86
test ( 'display multiline string diff' , ( ) => {
94
87
const string1 = 'string1\nstring2\nstring3'
95
88
const string2 = 'string2\nstring2\nstring1'
96
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
97
- displayDiff ( diff ( string1 , string2 ) , console as any )
98
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
89
+ expect ( wrapDiff ( diff ( string1 , string2 ) ) ) . toMatchInlineSnapshot ( `
99
90
"
100
91
- Expected
101
92
+ Received
@@ -112,9 +103,7 @@ test('display multiline string diff', () => {
112
103
test ( 'display truncated multiline string diff' , ( ) => {
113
104
const string1 = 'string1\nstring2\nstring3'
114
105
const string2 = 'string2\nstring2\nstring1'
115
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
116
- displayDiff ( diff ( string1 , string2 , { truncateThreshold : 2 } ) , console as any )
117
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
106
+ expect ( wrapDiff ( diff ( string1 , string2 , { truncateThreshold : 2 } ) ) ) . toMatchInlineSnapshot ( `
118
107
"
119
108
- Expected
120
109
+ Received
@@ -130,9 +119,7 @@ test('display truncated multiline string diff', () => {
130
119
test ( 'display truncated multiple items array diff' , ( ) => {
131
120
const array1 = Array . from ( { length : 45000 } ) . fill ( 'foo' )
132
121
const array2 = Array . from ( { length : 45000 } ) . fill ( 'bar' )
133
- const console = { log : vi . fn ( ) , error : vi . fn ( ) }
134
- displayDiff ( diff ( array1 , array2 , { truncateThreshold : 3 } ) , console as any )
135
- expect ( stripVTControlCharacters ( console . error . mock . calls [ 0 ] [ 0 ] ) ) . toMatchInlineSnapshot ( `
122
+ expect ( wrapDiff ( diff ( array1 , array2 , { truncateThreshold : 3 } ) ) ) . toMatchInlineSnapshot ( `
136
123
"
137
124
- Expected
138
125
+ Received
0 commit comments