MCPcopy
hub / github.com/jestjs/jest / formatStackTrace

Function formatStackTrace

packages/jest-message-util/src/index.ts:341–384  ·  view source on GitHub ↗
(
  stack: string,
  config: StackTraceConfig,
  options: StackTraceOptions,
  testPath?: string,
)

Source from the content-addressed store, hash-verified

339}
340
341export function formatStackTrace(
342 stack: string,
343 config: StackTraceConfig,
344 options: StackTraceOptions,
345 testPath?: string,
346): string {
347 const lines = getStackTraceLines(stack, options);
348 let renderedCallsite = '';
349 const relativeTestPath = testPath
350 ? slash(path.relative(config.rootDir, testPath))
351 : null;
352
353 if (!options.noStackTrace && !options.noCodeFrame) {
354 const topFrame = getTopFrame(lines);
355 if (topFrame) {
356 const {column, file: filename, line} = topFrame;
357
358 if (line && filename && path.isAbsolute(filename)) {
359 let fileContent;
360 try {
361 // TODO: check & read HasteFS instead of reading the filesystem:
362 // see: https://github.com/jestjs/jest/pull/5405#discussion_r164281696
363 fileContent = fs.readFileSync(filename, 'utf8');
364 renderedCallsite = getRenderedCallsite(fileContent, line, column);
365 } catch {
366 // the file does not exist or is inaccessible, we ignore
367 }
368 }
369 }
370 }
371
372 const stacktrace =
373 lines.length === 0
374 ? ''
375 : `\n${lines
376 .map(
377 line =>
378 STACK_INDENT +
379 formatPath(trimPaths(line), config, relativeTestPath),
380 )
381 .join('\n')}`;
382
383 return renderedCallsite + stacktrace;
384}
385
386type FailedResults = Array<{
387 /** Stringified version of the error */

Callers 9

_checkFakeTimersMethod · 0.90
_checkFakeTimersMethod · 0.90
getConsoleOutputFunction · 0.90
formatStackFunction · 0.90
printTestFileHeaderMethod · 0.90
formatExecErrorFunction · 0.85
formatErrorStackFunction · 0.85
messages.test.tsFile · 0.85

Calls 5

getStackTraceLinesFunction · 0.85
getTopFrameFunction · 0.85
getRenderedCallsiteFunction · 0.85
formatPathFunction · 0.85
trimPathsFunction · 0.85

Tested by

no test coverage detected