Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9d333a

Browse files
Tests: improve diffing for values of different types
Closegh-5454Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
1 parent063831b commitb9d333a

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

‎test/runner/listeners.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
returnnu;
5454
}
5555
}
56+
57+
// Serialize Symbols as string representations so they are
58+
// sent over the wire after being stringified.
59+
if(typeofvalue==="symbol"){
60+
61+
// We can *describe* unique symbols, but note that their identity
62+
// (e.g., `Symbol() !== Symbol()`) is lost
63+
varctor=Symbol.keyFor(value)!==undefined ?"Symbol.for" :"Symbol";
64+
returnctor+"("+JSON.stringify(value.description)+")";
65+
}
66+
5667
returnvalue;
5768
}
5869
returnderez(object);

‎test/runner/reporter.js

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { getBrowserString } from "./lib/getBrowserString.js";
33
import{prettyMs}from"./lib/prettyMs.js";
44
import*asDifffrom"diff";
55

6+
functionserializeForDiff(value){
7+
8+
// Use naive serialization for everything except types with confusable values
9+
if(typeofvalue==="string"){
10+
returnJSON.stringify(value);
11+
}
12+
if(typeofvalue==="bigint"){
13+
return`${value}n`;
14+
}
15+
return`${value}`;
16+
}
17+
618
exportfunctionreportTest(test,reportId,{ browser, headless}){
719
if(test.status==="passed"){
820

@@ -25,15 +37,19 @@ export function reportTest( test, reportId, { browser, headless } ) {
2537
message+=`\n${error.message}`;
2638
}
2739
message+=`\n${chalk.gray(error.stack)}`;
28-
if("expected"inerror&&"actual"inerror){
29-
message+=`\nexpected:${JSON.stringify(error.expected)}`;
30-
message+=`\nactual:${JSON.stringify(error.actual)}`;
40+
41+
// Show expected and actual values
42+
// if either is defined and non-null.
43+
// error.actual is set to null for failed
44+
// assert.expect() assertions, so skip those as well.
45+
// This should be fine because error.expected would
46+
// have to also be null for this to be skipped.
47+
if(error.expected!=null||error.actual!=null){
48+
message+=`\nexpected:${chalk.red(JSON.stringify(error.expected))}`;
49+
message+=`\nactual:${chalk.green(JSON.stringify(error.actual))}`;
3150
letdiff;
3251

33-
if(
34-
Array.isArray(error.expected)&&
35-
Array.isArray(error.actual)
36-
){
52+
if(Array.isArray(error.expected)&&Array.isArray(error.actual)){
3753

3854
// Diff arrays
3955
diff=Diff.diffArrays(error.expected,error.actual);
@@ -46,7 +62,7 @@ export function reportTest( test, reportId, { browser, headless } ) {
4662
diff=Diff.diffJson(error.expected,error.actual);
4763
}elseif(
4864
typeoferror.expected==="number"&&
49-
typeoferror.expected==="number"
65+
typeoferror.actual==="number"
5066
){
5167

5268
// Diff numbers directly
@@ -57,30 +73,35 @@ export function reportTest( test, reportId, { browser, headless } ) {
5773
diff=[{removed:true,value:`${value}`}];
5874
}
5975
}elseif(
60-
typeoferror.expected==="boolean"&&
61-
typeoferror.actual==="boolean"
76+
typeoferror.expected==="string"&&
77+
typeoferror.actual==="string"
6278
){
6379

64-
//Show theactual boolean in red
65-
diff=[{removed:true,value:`${error.actual}`}];
80+
//Diff thecharacters of strings
81+
diff=Diff.diffChars(error.expected,error.actual);
6682
}else{
6783

68-
// Diff everything else as characters
69-
diff=Diff.diffChars(`${error.expected}`,`${error.actual}`);
84+
// Diff everything else as words
85+
diff=Diff.diffWords(
86+
serializeForDiff(error.expected),
87+
serializeForDiff(error.actual)
88+
);
7089
}
7190

72-
message+="\n";
73-
message+=diff
74-
.map((part)=>{
75-
if(part.added){
76-
returnchalk.green(part.value);
77-
}
78-
if(part.removed){
79-
returnchalk.red(part.value);
80-
}
81-
returnchalk.gray(part.value);
82-
})
83-
.join("");
91+
if(diff){
92+
message+="\n";
93+
message+=diff
94+
.map((part)=>{
95+
if(part.added){
96+
returnchalk.green(part.value);
97+
}
98+
if(part.removed){
99+
returnchalk.red(part.value);
100+
}
101+
returnchalk.gray(part.value);
102+
})
103+
.join("");
104+
}
84105
}
85106
}
86107
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp