diff options
author | Timmy Willison <timmywil@users.noreply.github.com> | 2024-03-14 14:28:20 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-14 14:28:20 -0400 |
commit | 44fb7fa220e2dc2780203b128df2181853b3300f (patch) | |
tree | cdf5414144e01093e39aa1a47b3a7a997ecf5f93 /test/runner/reporter.js | |
parent | 1e84908baf13da63c33ee66c857e45c2f02eced7 (diff) | |
download | jquery-44fb7fa220e2dc2780203b128df2181853b3300f.tar.gz jquery-44fb7fa220e2dc2780203b128df2181853b3300f.zip |
Tests: add diffing to test reporter
Close gh-5445
Diffstat (limited to 'test/runner/reporter.js')
-rw-r--r-- | test/runner/reporter.js | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/test/runner/reporter.js b/test/runner/reporter.js index 74417f2fb..519d74709 100644 --- a/test/runner/reporter.js +++ b/test/runner/reporter.js @@ -1,6 +1,7 @@ import chalk from "chalk"; import { getBrowserString } from "./lib/getBrowserString.js"; import { prettyMs } from "./lib/prettyMs.js"; +import * as Diff from "diff"; export function reportTest( test, reportId, { browser, headless } ) { if ( test.status === "passed" ) { @@ -24,12 +25,58 @@ export function reportTest( test, reportId, { browser, headless } ) { message += `\n${ chalk.gray( error.stack ) }`; if ( error.expected && error.actual ) { message += `\nexpected: ${ JSON.stringify( error.expected ) }`; - message += `\nactual: ${ chalk.red( JSON.stringify( error.actual ) ) }`; + message += `\nactual: ${ JSON.stringify( error.actual ) }`; + let diff; + + if ( + Array.isArray( error.expected ) && + Array.isArray( error.actual ) + ) { + + // Diff arrays + diff = Diff.diffArrays( error.expected, error.actual ); + } else if ( + typeof error.expected === "object" && + typeof error.actual === "object" + ) { + + // Diff objects + diff = Diff.diffJson( error.expected, error.actual ); + } else if ( + typeof error.expected === "number" && + typeof error.expected === "number" + ) { + + // Diff numbers directly + const value = error.actual - error.expected; + if ( value > 0 ) { + diff = [ { added: true, value: `+${ value }` } ]; + } else { + diff = [ { removed: true, value: `${ value }` } ]; + } + } else { + + // Diff everything else as characters + diff = Diff.diffChars( `${ error.expected }`, `${ error.actual }` ); + } + + message += "\n"; + message += diff + .map( ( part ) => { + if ( part.added ) { + return chalk.green( part.value ); + } + if ( part.removed ) { + return chalk.red( part.value ); + } + return chalk.gray( part.value ); + } ) + .join( "" ); } } } - console.log( "\n\n" + message ); + console.log( `\n\n${ message }` ); // Only return failed messages if ( test.status === "failed" ) { |