1 import chalk from "chalk";
2 import { getBrowserString } from "./lib/getBrowserString.js";
3 import { prettyMs } from "./lib/prettyMs.js";
4 import * as Diff from "diff";
6 export function reportTest( test, reportId, { browser, headless } ) {
7 if ( test.status === "passed" ) {
9 // Write to console without newlines
10 process.stdout.write( "." );
14 let message = `${ chalk.bold( `${ test.suiteName }: ${ test.name }` ) }`;
15 message += `\nTest ${ test.status } on ${ chalk.yellow(
16 getBrowserString( browser, headless )
17 ) } (${ chalk.bold( reportId ) }).`;
19 // test.assertions only contains passed assertions;
20 // test.errors contains all failed asssertions
21 if ( test.errors.length ) {
22 for ( const error of test.errors ) {
24 if ( error.message ) {
25 message += `\n${ error.message }`;
27 message += `\n${ chalk.gray( error.stack ) }`;
28 if ( "expected" in error && "actual" in error ) {
29 message += `\nexpected: ${ JSON.stringify( error.expected ) }`;
30 message += `\nactual: ${ JSON.stringify( error.actual ) }`;
34 Array.isArray( error.expected ) &&
35 Array.isArray( error.actual )
39 diff = Diff.diffArrays( error.expected, error.actual );
41 typeof error.expected === "object" &&
42 typeof error.actual === "object"
46 diff = Diff.diffJson( error.expected, error.actual );
48 typeof error.expected === "number" &&
49 typeof error.expected === "number"
52 // Diff numbers directly
53 const value = error.actual - error.expected;
55 diff = [ { added: true, value: `+${ value }` } ];
57 diff = [ { removed: true, value: `${ value }` } ];
60 typeof error.expected === "boolean" &&
61 typeof error.actual === "boolean"
64 // Show the actual boolean in red
65 diff = [ { removed: true, value: `${ error.actual }` } ];
68 // Diff everything else as characters
69 diff = Diff.diffChars( `${ error.expected }`, `${ error.actual }` );
76 return chalk.green( part.value );
79 return chalk.red( part.value );
81 return chalk.gray( part.value );
88 console.log( `\n\n${ message }` );
90 // Only return failed messages
91 if ( test.status === "failed" ) {
96 export function reportEnd( result, reportId, { browser, headless, modules } ) {
98 `\n\nTests for ${ chalk.yellow( modules.join( ", " ) ) } on ${ chalk.yellow(
99 getBrowserString( browser, headless )
100 ) } finished in ${ prettyMs( result.runtime ) } (${ chalk.bold( reportId ) }).`
103 ( result.status !== "passed" ?
104 `${ chalk.red( result.testCounts.failed ) } failed. ` :
106 `${ chalk.green( result.testCounts.total ) } passed. ` +
107 `${ chalk.gray( result.testCounts.skipped ) } skipped.`
109 return result.testCounts;