diff --git a/packages/jest-message-util/src/index.js b/packages/jest-message-util/src/index.js index 6cf207c2a731..cdd253dae69a 100644 --- a/packages/jest-message-util/src/index.js +++ b/packages/jest-message-util/src/index.js @@ -44,6 +44,9 @@ type StackTraceOptions = { noStackTrace: boolean, }; +const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`; +const PATH_EXPECT_BUILD = `${path.sep}expect${path.sep}build${path.sep}`; + // filter for noisy stack trace lines const JASMINE_IGNORE = /^\s+at(?:(?:.*?vendor\/|jasmine\-)|\s+jasmine\.buildExpectationResult)/; const JEST_INTERNALS_IGNORE = /^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/; @@ -215,6 +218,22 @@ const formatPaths = ( return STACK_TRACE_COLOR(match[1]) + filePath + STACK_TRACE_COLOR(match[3]); }; +const getTopFrame = (lines: string[]) => { + for (const line of lines) { + if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_EXPECT_BUILD)) { + continue; + } + + const parsedFrame = stackUtils.parseLine(line.trim()); + + if (parsedFrame && parsedFrame.file) { + return parsedFrame; + } + } + + return null; +}; + export const formatStackTrace = ( stack: string, config: StackTraceConfig, @@ -228,17 +247,7 @@ export const formatStackTrace = ( : null; lines = removeInternalStackEntries(lines, options); - const topFrame = lines - .map(line => line.trim()) - .filter(Boolean) - .filter( - line => - !line.includes(`${path.sep}node_modules${path.sep}`) && - !line.includes(`${path.sep}expect${path.sep}build${path.sep}`), - ) - .map(line => stackUtils.parseLine(line)) - .filter(Boolean) - .filter(parsedFrame => parsedFrame.file)[0]; + const topFrame = getTopFrame(lines); if (topFrame) { const filename = topFrame.file;