@@ -9,6 +9,7 @@ import * as vscode from 'vscode'
9
9
import { getLogger } from '../shared/logger'
10
10
import { hasKey } from '../shared/utilities/tsUtils'
11
11
import { getTestWindow , printPendingUiElements } from './shared/vscode/window'
12
+ import { ToolkitError , formatError } from '../shared/errors'
12
13
13
14
const runnableTimeout = Symbol ( 'runnableTimeout' )
14
15
@@ -54,6 +55,49 @@ export function setRunnableTimeout(test: Mocha.Runnable, maxTestDuration: number
54
55
return test
55
56
}
56
57
58
+ export function skipTest ( testOrCtx : Mocha . Context | Mocha . Test | undefined , reason ?: string ) {
59
+ const test =
60
+ testOrCtx ?. type === 'test' ? ( testOrCtx as Mocha . Test ) : ( testOrCtx as Mocha . Context | undefined ) ?. currentTest
61
+
62
+ if ( test ) {
63
+ test . title += ` (skipped${ reason ? ` - ${ reason } ` : '' } )`
64
+ test . skip ( )
65
+ }
66
+ }
67
+
68
+ export function skipSuite ( suite : Mocha . Suite , reason ?: string ) {
69
+ suite . eachTest ( test => skipTest ( test , reason ) )
70
+ }
71
+
72
+ export function mapTestErrors ( runner : Mocha . Runner , fn : ( err : unknown , test : Mocha . Test ) => any ) {
73
+ return runner . prependListener ( 'fail' , ( test , err ) => {
74
+ test . err = fn ( err , test ) || err
75
+ } )
76
+ }
77
+
78
+ /**
79
+ * Formats any known sub-classes of {@link Error} for better compatability with test reporters.
80
+ *
81
+ * Most test reporters will only output the name + message + stack trace so any relevant
82
+ * info must go into those fields.
83
+ */
84
+ export function normalizeError ( err ?: unknown ) {
85
+ if ( err instanceof ToolkitError ) {
86
+ // Error has to be mutated to show up in the report:
87
+ // https://github.com/michaelleeallen/mocha-junit-reporter/blob/4b17772f8da33d580fafa4d124e5c11142a70c1f/index.js#L262
88
+ //
89
+ // We'll just patch the message/stack trace even though it's arguably incorrect (and looks kind of ugly)
90
+ // Once `cause` is more common in the JS ecosystem we'll start to see support from test reporters
91
+
92
+ return Object . assign ( err , {
93
+ message : formatError ( err ) . replace ( `${ err . name } : ` , '' ) ,
94
+ stack : err . stack ?. replace ( err . message , err . trace . replace ( `${ err . name } : ` , '' ) + '\n' ) ,
95
+ } )
96
+ }
97
+
98
+ return err
99
+ }
100
+
57
101
export function patchObject < T extends Record < string , any > , U extends keyof T > (
58
102
obj : T ,
59
103
key : U ,
0 commit comments