1- import { diff } from 'jest-diff' ;
2- import {
3- format as prettyFormat ,
4- plugins as prettyFormatPlugins ,
5- } from 'pretty-format' ;
61import type { FormattedError , Test } from '../types' ;
72
3+ const loadDiffModules = async ( ) => {
4+ const [ jestDiff , prettyFormat ] = await Promise . all ( [
5+ import ( 'jest-diff' ) ,
6+ import ( 'pretty-format' ) ,
7+ ] ) ;
8+
9+ return {
10+ diff : jestDiff . diff ,
11+ prettyFormat : prettyFormat . format ,
12+ prettyFormatPlugins : prettyFormat . plugins ,
13+ } ;
14+ } ;
15+
816const REAL_TIMERS : {
917 setTimeout ?: typeof globalThis . setTimeout ;
1018 clearTimeout ?: typeof globalThis . clearTimeout ;
@@ -20,49 +28,59 @@ export const getRealTimers = (): typeof REAL_TIMERS => {
2028 return REAL_TIMERS ;
2129} ;
2230
23- export const formatTestError = ( err : any , test ?: Test ) : FormattedError [ ] => {
31+ export const formatTestError = async (
32+ err : any ,
33+ test ?: Test ,
34+ ) : Promise < FormattedError [ ] > => {
2435 const errors = Array . isArray ( err ) ? err : [ err ] ;
2536
26- return errors . map ( ( rawError ) => {
27- const error =
28- typeof rawError === 'string' ? { message : rawError } : rawError ;
29- const errObj : FormattedError = {
30- fullStack : error . fullStack ,
31- // Some error attributes cannot be enumerated
32- message : error . message ,
33- name : error . name ,
34- stack : error . stack ,
35- } ;
36-
37- if ( error instanceof TestRegisterError && test ?. type === 'case' ) {
38- errObj . message = `Can't nest describe or test inside a test. ${ error . message } because it is nested within test '${ test . name } '` ;
39- }
40-
41- if (
42- error . showDiff ||
43- ( error . showDiff === undefined &&
44- error . expected !== undefined &&
45- error . actual !== undefined )
46- ) {
47- errObj . diff = diff ( err . expected , err . actual , {
48- expand : false ,
49- } ) ! ;
50- errObj . expected =
51- typeof error . expected === 'string'
52- ? error . expected
53- : prettyFormat ( error . expected , {
54- plugins : Object . values ( prettyFormatPlugins ) ,
55- } ) ;
56- errObj . actual =
57- typeof error . actual === 'string'
58- ? error . actual
59- : prettyFormat ( error . actual , {
60- plugins : Object . values ( prettyFormatPlugins ) ,
61- } ) ;
62- }
63-
64- return errObj ;
65- } ) ;
37+ return Promise . all (
38+ errors . map ( async ( rawError ) => {
39+ const error =
40+ typeof rawError === 'string' ? { message : rawError } : rawError ;
41+ const errObj : FormattedError = {
42+ fullStack : error . fullStack ,
43+ // Some error attributes cannot be enumerated
44+ message : error . message ,
45+ name : error . name ,
46+ stack : error . stack ,
47+ } ;
48+
49+ if ( error instanceof TestRegisterError && test ?. type === 'case' ) {
50+ errObj . message = `Can't nest describe or test inside a test. ${ error . message } because it is nested within test '${ test . name } '` ;
51+ }
52+
53+ if (
54+ error . showDiff ||
55+ ( error . showDiff === undefined &&
56+ error . expected !== undefined &&
57+ error . actual !== undefined )
58+ ) {
59+ const expected = error . expected ;
60+ const actual = error . actual ;
61+ const { diff, prettyFormat, prettyFormatPlugins } =
62+ await loadDiffModules ( ) ;
63+
64+ errObj . diff = diff ( expected , actual , {
65+ expand : false ,
66+ } ) ! ;
67+ errObj . expected =
68+ typeof expected === 'string'
69+ ? expected
70+ : prettyFormat ( expected , {
71+ plugins : Object . values ( prettyFormatPlugins ) ,
72+ } ) ;
73+ errObj . actual =
74+ typeof actual === 'string'
75+ ? actual
76+ : prettyFormat ( actual , {
77+ plugins : Object . values ( prettyFormatPlugins ) ,
78+ } ) ;
79+ }
80+
81+ return errObj ;
82+ } ) ,
83+ ) ;
6684} ;
6785// cspell:ignore sdjifo
6886const formatRegExp = / % [ s d j i f o O c % ] / ;
0 commit comments