3
3
*
4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow
8
6
*/
9
7
10
- import type { Glob , Path } from 'types/Config' ;
11
- import type { AssertionResult , SerializableError } from 'types/TestResult' ;
12
-
13
8
import fs from 'fs' ;
14
9
import path from 'path' ;
10
+ import { Config , TestResult } from '@jest/types' ;
15
11
import chalk from 'chalk' ;
16
12
import micromatch from 'micromatch' ;
17
13
import slash from 'slash' ;
18
14
import { codeFrameColumns } from '@babel/code-frame' ;
19
15
import StackUtils from 'stack-utils' ;
20
16
17
+ type Glob = Config . Glob ;
18
+ type Path = Config . Path ;
19
+ type AssertionResult = TestResult . AssertionResult ;
20
+ type SerializableError = TestResult . SerializableError ;
21
+
21
22
// stack utils tries to create pretty stack by making paths relative.
22
23
const stackUtils = new StackUtils ( {
23
24
cwd : 'something which does not exist' ,
24
25
} ) ;
25
26
26
- let nodeInternals = [ ] ;
27
+ let nodeInternals : RegExp [ ] = [ ] ;
27
28
28
29
try {
29
30
nodeInternals = StackUtils . nodeInternals ( ) ;
@@ -33,12 +34,12 @@ try {
33
34
}
34
35
35
36
type StackTraceConfig = {
36
- rootDir : string ,
37
- testMatch : Array < Glob > ,
37
+ rootDir : string ;
38
+ testMatch : Array < Glob > ;
38
39
} ;
39
40
40
41
type StackTraceOptions = {
41
- noStackTrace : boolean ,
42
+ noStackTrace : boolean ;
42
43
} ;
43
44
44
45
const PATH_NODE_MODULES = `${ path . sep } node_modules${ path . sep } ` ;
@@ -64,13 +65,13 @@ const NOT_EMPTY_LINE_REGEXP = /^(?!$)/gm;
64
65
const indentAllLines = ( lines : string , indent : string ) =>
65
66
lines . replace ( NOT_EMPTY_LINE_REGEXP , indent ) ;
66
67
67
- const trim = string => ( string || '' ) . trim ( ) ;
68
+ const trim = ( string : string ) => ( string || '' ) . trim ( ) ;
68
69
69
70
// Some errors contain not only line numbers in stack traces
70
71
// e.g. SyntaxErrors can contain snippets of code, and we don't
71
72
// want to trim those, because they may have pointers to the column/character
72
73
// which will get misaligned.
73
- const trimPaths = string =>
74
+ const trimPaths = ( string : string ) =>
74
75
string . match ( STACK_PATH_REGEXP ) ? trim ( string ) : string ;
75
76
76
77
const getRenderedCallsite = (
@@ -94,11 +95,11 @@ const getRenderedCallsite = (
94
95
// `before/after each` hooks). If it's thrown, none of the tests in the file
95
96
// are executed.
96
97
export const formatExecError = (
97
- error ? : Error | SerializableError | string ,
98
+ error : Error | SerializableError | string | undefined ,
98
99
config : StackTraceConfig ,
99
100
options : StackTraceOptions ,
100
- testPath : ? Path ,
101
- reuseMessage : ? boolean ,
101
+ testPath ?: Path ,
102
+ reuseMessage ?: boolean ,
102
103
) => {
103
104
if ( ! error || typeof error === 'number' ) {
104
105
error = new Error ( `Expected an Error, but "${ String ( error ) } " was thrown` ) ;
@@ -198,7 +199,11 @@ const removeInternalStackEntries = (
198
199
} ) ;
199
200
} ;
200
201
201
- const formatPaths = ( config : StackTraceConfig , relativeTestPath , line ) => {
202
+ const formatPaths = (
203
+ config : StackTraceConfig ,
204
+ relativeTestPath : Path | null ,
205
+ line : string ,
206
+ ) => {
202
207
// Extract the file path from the trace line.
203
208
const match = line . match ( / ( ^ \s * a t .* ?\( ? ) ( [ ^ ( ) ] + ) ( : [ 0 - 9 ] + : [ 0 - 9 ] + \) ? .* $ ) / ) ;
204
209
if ( ! match ) {
@@ -243,7 +248,7 @@ export const formatStackTrace = (
243
248
stack : string ,
244
249
config : StackTraceConfig ,
245
250
options : StackTraceOptions ,
246
- testPath : ? Path ,
251
+ testPath ?: Path ,
247
252
) => {
248
253
const lines = getStackTraceLines ( stack , options ) ;
249
254
const topFrame = getTopFrame ( lines ) ;
@@ -253,19 +258,15 @@ export const formatStackTrace = (
253
258
: null ;
254
259
255
260
if ( topFrame ) {
256
- const filename = topFrame . file ;
261
+ const { column , file : filename , line } = topFrame ;
257
262
258
- if ( path . isAbsolute ( filename ) ) {
263
+ if ( line && filename && path . isAbsolute ( filename ) ) {
259
264
let fileContent ;
260
265
try {
261
266
// TODO: check & read HasteFS instead of reading the filesystem:
262
267
// see: https://github.com/facebook/jest/pull/5405#discussion_r164281696
263
268
fileContent = fs . readFileSync ( filename , 'utf8' ) ;
264
- renderedCallsite = getRenderedCallsite (
265
- fileContent ,
266
- topFrame . line ,
267
- topFrame . column ,
268
- ) ;
269
+ renderedCallsite = getRenderedCallsite ( fileContent , line , column ) ;
269
270
} catch ( e ) {
270
271
// the file does not exist or is inaccessible, we ignore
271
272
}
@@ -287,12 +288,20 @@ export const formatResultsErrors = (
287
288
testResults : Array < AssertionResult > ,
288
289
config : StackTraceConfig ,
289
290
options : StackTraceOptions ,
290
- testPath : ?Path ,
291
- ) : ?string => {
292
- const failedResults = testResults . reduce ( ( errors , result ) => {
293
- result . failureMessages . forEach ( content => errors . push ( { content, result} ) ) ;
294
- return errors ;
295
- } , [ ] ) ;
291
+ testPath ?: Path ,
292
+ ) : string | null => {
293
+ type FailedResults = Array < {
294
+ content : string ;
295
+ result : AssertionResult ;
296
+ } > ;
297
+
298
+ const failedResults : FailedResults = testResults . reduce (
299
+ ( errors , result ) => {
300
+ result . failureMessages . forEach ( content => errors . push ( { content, result} ) ) ;
301
+ return errors ;
302
+ } ,
303
+ [ ] as FailedResults ,
304
+ ) ;
296
305
297
306
if ( ! failedResults . length ) {
298
307
return null ;
0 commit comments