1
1
import chalk from "chalk" ;
2
2
import { getBrowserString } from "./lib/getBrowserString.js" ;
3
3
import { prettyMs } from "./lib/prettyMs.js" ;
4
+ import * as Diff from "diff" ;
4
5
5
6
export function reportTest ( test , reportId , { browser, headless } ) {
6
7
if ( test . status === "passed" ) {
@@ -24,12 +25,58 @@ export function reportTest( test, reportId, { browser, headless } ) {
24
25
message += `\n${ chalk . gray ( error . stack ) } ` ;
25
26
if ( error . expected && error . actual ) {
26
27
message += `\nexpected: ${ JSON . stringify ( error . expected ) } ` ;
27
- message += `\nactual: ${ chalk . red ( JSON . stringify ( error . actual ) ) } ` ;
28
+ message += `\nactual: ${ JSON . stringify ( error . actual ) } ` ;
29
+ let diff ;
30
+
31
+ if (
32
+ Array . isArray ( error . expected ) &&
33
+ Array . isArray ( error . actual )
34
+ ) {
35
+
36
+ // Diff arrays
37
+ diff = Diff . diffArrays ( error . expected , error . actual ) ;
38
+ } else if (
39
+ typeof error . expected === "object" &&
40
+ typeof error . actual === "object"
41
+ ) {
42
+
43
+ // Diff objects
44
+ diff = Diff . diffJson ( error . expected , error . actual ) ;
45
+ } else if (
46
+ typeof error . expected === "number" &&
47
+ typeof error . expected === "number"
48
+ ) {
49
+
50
+ // Diff numbers directly
51
+ const value = error . actual - error . expected ;
52
+ if ( value > 0 ) {
53
+ diff = [ { added : true , value : `+${ value } ` } ] ;
54
+ } else {
55
+ diff = [ { removed : true , value : `${ value } ` } ] ;
56
+ }
57
+ } else {
58
+
59
+ // Diff everything else as characters
60
+ diff = Diff . diffChars ( `${ error . expected } ` , `${ error . actual } ` ) ;
61
+ }
62
+
63
+ message += "\n" ;
64
+ message += diff
65
+ . map ( ( part ) => {
66
+ if ( part . added ) {
67
+ return chalk . green ( part . value ) ;
68
+ }
69
+ if ( part . removed ) {
70
+ return chalk . red ( part . value ) ;
71
+ }
72
+ return chalk . gray ( part . value ) ;
73
+ } )
74
+ . join ( "" ) ;
28
75
}
29
76
}
30
77
}
31
78
32
- console . log ( " \n\n" + message ) ;
79
+ console . log ( ` \n\n${ message } ` ) ;
33
80
34
81
// Only return failed messages
35
82
if ( test . status === "failed" ) {
0 commit comments