33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6- *
7- * @flow
86 */
97
10- import type { Glob , Path } from 'types/Config' ;
11- import type { AssertionResult , SerializableError } from 'types/TestResult' ;
12-
138import fs from 'fs' ;
149import path from 'path' ;
10+ import { Config , TestResult } from '@jest/types' ;
1511import chalk from 'chalk' ;
1612import micromatch from 'micromatch' ;
1713import slash from 'slash' ;
1814import { codeFrameColumns } from '@babel/code-frame' ;
1915import StackUtils from 'stack-utils' ;
2016
17+ type Glob = Config . Glob ;
18+ type Path = Config . Path ;
19+ type AssertionResult = TestResult . AssertionResult ;
20+ type SerializableError = TestResult . SerializableError ;
21+
2122// stack utils tries to create pretty stack by making paths relative.
2223const stackUtils = new StackUtils ( {
2324 cwd : 'something which does not exist' ,
2425} ) ;
2526
26- let nodeInternals = [ ] ;
27+ let nodeInternals : RegExp [ ] = [ ] ;
2728
2829try {
2930 nodeInternals = StackUtils . nodeInternals ( ) ;
@@ -33,12 +34,12 @@ try {
3334}
3435
3536type StackTraceConfig = {
36- rootDir : string ,
37- testMatch : Array < Glob > ,
37+ rootDir : string ;
38+ testMatch : Array < Glob > ;
3839} ;
3940
4041type StackTraceOptions = {
41- noStackTrace : boolean ,
42+ noStackTrace : boolean ;
4243} ;
4344
4445const PATH_NODE_MODULES = `${ path . sep } node_modules${ path . sep } ` ;
@@ -64,13 +65,13 @@ const NOT_EMPTY_LINE_REGEXP = /^(?!$)/gm;
6465const indentAllLines = ( lines : string , indent : string ) =>
6566 lines . replace ( NOT_EMPTY_LINE_REGEXP , indent ) ;
6667
67- const trim = string => ( string || '' ) . trim ( ) ;
68+ const trim = ( string : string ) => ( string || '' ) . trim ( ) ;
6869
6970// Some errors contain not only line numbers in stack traces
7071// e.g. SyntaxErrors can contain snippets of code, and we don't
7172// want to trim those, because they may have pointers to the column/character
7273// which will get misaligned.
73- const trimPaths = string =>
74+ const trimPaths = ( string : string ) =>
7475 string . match ( STACK_PATH_REGEXP ) ? trim ( string ) : string ;
7576
7677const getRenderedCallsite = (
@@ -94,11 +95,11 @@ const getRenderedCallsite = (
9495// `before/after each` hooks). If it's thrown, none of the tests in the file
9596// are executed.
9697export const formatExecError = (
97- error ? : Error | SerializableError | string ,
98+ error : Error | SerializableError | string | undefined ,
9899 config : StackTraceConfig ,
99100 options : StackTraceOptions ,
100- testPath : ? Path ,
101- reuseMessage : ? boolean ,
101+ testPath ?: Path ,
102+ reuseMessage ?: boolean ,
102103) => {
103104 if ( ! error || typeof error === 'number' ) {
104105 error = new Error ( `Expected an Error, but "${ String ( error ) } " was thrown` ) ;
@@ -198,7 +199,11 @@ const removeInternalStackEntries = (
198199 } ) ;
199200} ;
200201
201- const formatPaths = ( config : StackTraceConfig , relativeTestPath , line ) => {
202+ const formatPaths = (
203+ config : StackTraceConfig ,
204+ relativeTestPath : Path | null ,
205+ line : string ,
206+ ) => {
202207 // Extract the file path from the trace line.
203208 const match = line . match ( / ( ^ \s * a t .* ?\( ? ) ( [ ^ ( ) ] + ) ( : [ 0 - 9 ] + : [ 0 - 9 ] + \) ? .* $ ) / ) ;
204209 if ( ! match ) {
@@ -243,7 +248,7 @@ export const formatStackTrace = (
243248 stack : string ,
244249 config : StackTraceConfig ,
245250 options : StackTraceOptions ,
246- testPath : ? Path ,
251+ testPath ?: Path ,
247252) => {
248253 const lines = getStackTraceLines ( stack , options ) ;
249254 const topFrame = getTopFrame ( lines ) ;
@@ -253,19 +258,15 @@ export const formatStackTrace = (
253258 : null ;
254259
255260 if ( topFrame ) {
256- const filename = topFrame . file ;
261+ const { column , file : filename , line } = topFrame ;
257262
258- if ( path . isAbsolute ( filename ) ) {
263+ if ( line && filename && path . isAbsolute ( filename ) ) {
259264 let fileContent ;
260265 try {
261266 // TODO: check & read HasteFS instead of reading the filesystem:
262267 // see: https://github.com/facebook/jest/pull/5405#discussion_r164281696
263268 fileContent = fs . readFileSync ( filename , 'utf8' ) ;
264- renderedCallsite = getRenderedCallsite (
265- fileContent ,
266- topFrame . line ,
267- topFrame . column ,
268- ) ;
269+ renderedCallsite = getRenderedCallsite ( fileContent , line , column ) ;
269270 } catch ( e ) {
270271 // the file does not exist or is inaccessible, we ignore
271272 }
@@ -287,12 +288,20 @@ export const formatResultsErrors = (
287288 testResults : Array < AssertionResult > ,
288289 config : StackTraceConfig ,
289290 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+ ) ;
296305
297306 if ( ! failedResults . length ) {
298307 return null ;
0 commit comments