diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index 2ac94ac5c83b..68aa6fc0b98d 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -9,7 +9,8 @@ import assert = require('assert'); import {Console} from 'console'; import {format} from 'util'; import chalk = require('chalk'); -import {SourceMapRegistry, getCallsite} from '@jest/source-map'; +import {SourceMapRegistry, getSourceMappedStack} from '@jest/source-map'; + import type { ConsoleBuffer, LogCounters, @@ -48,8 +49,8 @@ export default class BufferedConsole extends Console { level?: number | null, sourceMaps?: SourceMapRegistry | null, ): ConsoleBuffer { - const callsite = getCallsite(level != null ? level : 2, sourceMaps); - const origin = callsite.getFileName() + ':' + callsite.getLineNumber(); + + const origin = getSourceMappedStack(level != null ? level : 2, sourceMaps); buffer.push({ message, diff --git a/packages/jest-console/src/formatStackTrace.ts b/packages/jest-console/src/formatStackTrace.ts new file mode 100644 index 000000000000..e27be016b9c7 --- /dev/null +++ b/packages/jest-console/src/formatStackTrace.ts @@ -0,0 +1,274 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * This is a temporary file to prevent breaking change + * TODO: + * 1) move formatResultErrors to jest-test-result so that jest-message-util no + * longer depends on jest-test-result + * 2) make jest-console depend on jest-message-util + * 3) remove this file + */ + +import * as fs from 'fs'; +import * as path from 'path'; +import type {Config} from '@jest/types'; +import chalk = require('chalk'); +import micromatch = require('micromatch'); +import slash = require('slash'); +import {codeFrameColumns} from '@babel/code-frame'; +import StackUtils = require('stack-utils'); + + +export interface Frame extends StackUtils.StackData { + file: string; +} + +type Path = Config.Path; + +// stack utils tries to create pretty stack by making paths relative. +const stackUtils = new StackUtils({cwd: 'something which does not exist'}); + +let nodeInternals: Array = []; + +try { + nodeInternals = StackUtils.nodeInternals(); +} catch (e) { + // `StackUtils.nodeInternals()` fails in browsers. We don't need to remove + // node internals in the browser though, so no issue. +} + +export type StackTraceConfig = Pick< + Config.ProjectConfig, + 'rootDir' | 'testMatch' +>; + +export type StackTraceOptions = { + noStackTrace: boolean; +}; + +const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`; +const PATH_JEST_PACKAGES = `${path.sep}jest${path.sep}packages${path.sep}`; + +// filter for noisy stack trace lines +const JASMINE_IGNORE = /^\s+at(?:(?:.jasmine\-)|\s+jasmine\.buildExpectationResult)/; +const JEST_INTERNALS_IGNORE = /^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/; +const ANONYMOUS_FN_IGNORE = /^\s+at .*$/; +const ANONYMOUS_PROMISE_IGNORE = /^\s+at (new )?Promise \(\).*$/; +const ANONYMOUS_GENERATOR_IGNORE = /^\s+at Generator.next \(\).*$/; +const NATIVE_NEXT_IGNORE = /^\s+at next \(native\).*$/; +const MESSAGE_INDENT = ' '; +const STACK_INDENT = ' '; +const STACK_TRACE_COLOR = chalk.dim; +const STACK_PATH_REGEXP = /\s*at.*\(?(\:\d*\:\d*|native)\)?/; +const NOT_EMPTY_LINE_REGEXP = /^(?!$)/gm; + +const indentAllLines = (lines: string, indent: string) => + lines.replace(NOT_EMPTY_LINE_REGEXP, indent); + +const trim = (string: string) => (string || '').trim(); + +// Some errors contain not only line numbers in stack traces +// e.g. SyntaxErrors can contain snippets of code, and we don't +// want to trim those, because they may have pointers to the column/character +// which will get misaligned. +const trimPaths = (string: string) => + string.match(STACK_PATH_REGEXP) ? trim(string) : string; + +const getRenderedCallsite = ( + fileContent: string, + line: number, + column?: number, +) => { + let renderedCallsite = codeFrameColumns( + fileContent, + {start: {column, line}}, + {highlightCode: true}, + ); + + renderedCallsite = indentAllLines(renderedCallsite, MESSAGE_INDENT); + + renderedCallsite = `\n${renderedCallsite}\n`; + return renderedCallsite; +}; + + + +const removeInternalStackEntries = ( + lines: Array, + options: StackTraceOptions, +): Array => { + let pathCounter = 0; + + return lines.filter(line => { + if (ANONYMOUS_FN_IGNORE.test(line)) { + return false; + } + + if (ANONYMOUS_PROMISE_IGNORE.test(line)) { + return false; + } + + if (ANONYMOUS_GENERATOR_IGNORE.test(line)) { + return false; + } + + if (NATIVE_NEXT_IGNORE.test(line)) { + return false; + } + + if (nodeInternals.some(internal => internal.test(line))) { + return false; + } + + if (!STACK_PATH_REGEXP.test(line)) { + return true; + } + + if (JASMINE_IGNORE.test(line)) { + return false; + } + + if (++pathCounter === 1) { + return true; // always keep the first line even if it's from Jest + } + + if (options.noStackTrace) { + return false; + } + + if (JEST_INTERNALS_IGNORE.test(line)) { + return false; + } + + return true; + }); +}; + +const formatPaths = ( + config: StackTraceConfig, + relativeTestPath: Path | null, + line: string, +) => { + // Extract the file path from the trace line. + const match = line.match(/(^\s*at .*?\(?)([^()]+)(:[0-9]+:[0-9]+\)?.*$)/); + if (!match) { + return line; + } + + let filePath = slash(path.relative(config.rootDir, match[2])); + // highlight paths from the current test file + if ( + (config.testMatch && + config.testMatch.length && + micromatch([filePath], config.testMatch).length > 0) || + filePath === relativeTestPath + ) { + filePath = chalk.reset.cyan(filePath); + } + return STACK_TRACE_COLOR(match[1]) + filePath + STACK_TRACE_COLOR(match[3]); +}; + +export const getStackTraceLines = ( + stack: string, + options: StackTraceOptions = {noStackTrace: false}, +): Array => removeInternalStackEntries(stack.split(/\n/), options); + +export const getTopFrame = (lines: Array): Frame | null => { + for (const line of lines) { + if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_JEST_PACKAGES)) { + continue; + } + + const parsedFrame = stackUtils.parseLine(line.trim()); + + if (parsedFrame && parsedFrame.file) { + return parsedFrame as Frame; + } + } + + return null; +}; + +export const formatStackTrace = ( + stack: string, + config: StackTraceConfig, + options: StackTraceOptions, + testPath?: Path, +): string => { + const lines = getStackTraceLines(stack, options); + const topFrame = getTopFrame(lines); + let renderedCallsite = ''; + const relativeTestPath = testPath + ? slash(path.relative(config.rootDir, testPath)) + : null; + + if (topFrame) { + const {column, file: filename, line} = topFrame; + + if (line && filename && path.isAbsolute(filename)) { + let fileContent; + try { + // TODO: check & read HasteFS instead of reading the filesystem: + // see: https://github.com/facebook/jest/pull/5405#discussion_r164281696 + fileContent = fs.readFileSync(filename, 'utf8'); + renderedCallsite = getRenderedCallsite(fileContent, line, column); + } catch (e) { + // the file does not exist or is inaccessible, we ignore + } + } + } + + const stacktrace = lines + .filter(Boolean) + .map( + line => + STACK_INDENT + formatPaths(config, relativeTestPath, trimPaths(line)), + ) + .join('\n'); + + return `${renderedCallsite}\n${stacktrace}`; +}; + + + +const errorRegexp = /^Error:?\s*$/; + +export const removeBlankErrorLine = (str: string) => + str + .split('\n') + // Lines saying just `Error:` are useless + .filter(line => + !errorRegexp.test(line)) + .join('\n') + .trimRight(); + +// jasmine and worker farm sometimes don't give us access to the actual +// Error object, so we have to regexp out the message from the stack string +// to format it. +export const separateMessageFromStack = ( + content: string, +): {message: string; stack: string} => { + if (!content) { + return {message: '', stack: ''}; + } + + // All lines up to what looks like a stack -- or if nothing looks like a stack + // (maybe it's a code frame instead), just the first non-empty line. + // If the error is a plain "Error:" instead of a SyntaxError or TypeError we + // remove the prefix from the message because it is generally not useful. + const messageMatch = content.match( + /^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/, + ); + if (!messageMatch) { + // For typescript + throw new Error('If you hit this error, the regex above is buggy.'); + } + const message = removeBlankErrorLine(messageMatch[1]); + const stack = removeBlankErrorLine(messageMatch[2]); + return {message, stack}; +}; diff --git a/packages/jest-console/src/getConsoleOutput.ts b/packages/jest-console/src/getConsoleOutput.ts index 09a3232b557e..bb518cae0ebf 100644 --- a/packages/jest-console/src/getConsoleOutput.ts +++ b/packages/jest-console/src/getConsoleOutput.ts @@ -5,43 +5,59 @@ * LICENSE file in the root directory of this source tree. */ -import * as path from 'path'; import chalk = require('chalk'); -import slash = require('slash'); import type {ConsoleBuffer} from './types'; +import { + formatStackTrace, + StackTraceConfig} from 'jest-message-util'; + export default ( - root: string, verbose: boolean, + stackTraceConfig: StackTraceConfig, buffer: ConsoleBuffer, ): string => { const TITLE_INDENT = verbose ? ' ' : ' '; const CONSOLE_INDENT = TITLE_INDENT + ' '; - return buffer.reduce((output, {type, message, origin}) => { - origin = slash(path.relative(root, origin)); + return buffer.reduce((output, {type, message, origin}) => { message = message .split(/\n/) .map(line => CONSOLE_INDENT + line) .join('\n'); let typeMessage = 'console.' + type; + let noStackTrace = true; + let noCodeFrame = true; + if (type === 'warn') { message = chalk.yellow(message); typeMessage = chalk.yellow(typeMessage); + noStackTrace = false; + noCodeFrame = false; } else if (type === 'error') { message = chalk.red(message); typeMessage = chalk.red(typeMessage); - } + noStackTrace = false; + noCodeFrame = false; + } + + const formattedStackTrace = formatStackTrace( + origin, + stackTraceConfig, + { + noStackTrace: noStackTrace, + noCodeFrame: noCodeFrame + }); return ( output + TITLE_INDENT + chalk.dim(typeMessage) + - ' ' + - chalk.dim(origin) + '\n' + message + + '\n' + + chalk.dim(formattedStackTrace) + '\n' ); }, ''); diff --git a/packages/jest-console/tsconfig.json b/packages/jest-console/tsconfig.json index 719bab0f554b..787fa0ffe52d 100644 --- a/packages/jest-console/tsconfig.json +++ b/packages/jest-console/tsconfig.json @@ -6,6 +6,7 @@ }, "references": [ {"path": "../jest-source-map"}, + {"path": "../jest-message-util"}, {"path": "../jest-util"} ] } diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index e515dcea6ab3..6b8318592d82 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -38,6 +38,7 @@ export type StackTraceConfig = Pick< export type StackTraceOptions = { noStackTrace: boolean; + noCodeFrame?: boolean; }; const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`; @@ -251,7 +252,7 @@ const formatPaths = ( export const getStackTraceLines = ( stack: string, - options: StackTraceOptions = {noStackTrace: false}, + options: StackTraceOptions = {noStackTrace: false, noCodeFrame: false}, ): Array => removeInternalStackEntries(stack.split(/\n/), options); export const getTopFrame = (lines: Array): Frame | null => { @@ -283,7 +284,7 @@ export const formatStackTrace = ( ? slash(path.relative(config.rootDir, testPath)) : null; - if (topFrame) { + if (topFrame && !options.noStackTrace) { const {column, file: filename, line} = topFrame; if (line && filename && path.isAbsolute(filename)) { @@ -307,7 +308,8 @@ export const formatStackTrace = ( ) .join('\n'); - return `${renderedCallsite}\n${stacktrace}`; + return (options.noCodeFrame) ? + `${stacktrace}` : `${renderedCallsite}\n${stacktrace}`; }; type FailedResults = Array<{ diff --git a/packages/jest-reporters/src/default_reporter.ts b/packages/jest-reporters/src/default_reporter.ts index ae90384cd2b8..8997791d899a 100644 --- a/packages/jest-reporters/src/default_reporter.ts +++ b/packages/jest-reporters/src/default_reporter.ts @@ -179,8 +179,8 @@ export default class DefaultReporter extends BaseReporter { TITLE_BULLET + 'Console\n\n' + getConsoleOutput( - config.cwd, !!this._globalConfig.verbose, + config, result.console, ), ); diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index a617bc9d510c..d1679d104d49 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -119,8 +119,8 @@ async function runTestInternal( const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout; const consoleFormatter = (type: LogType, message: LogMessage) => getConsoleOutput( - config.cwd, !!globalConfig.verbose, + config, // 4 = the console call is buried 4 stack frames deep BufferedConsole.write( [], diff --git a/packages/jest-source-map/src/getCallsite.ts b/packages/jest-source-map/src/getCallsite.ts index ecb903d7729e..ffba37e77718 100644 --- a/packages/jest-source-map/src/getCallsite.ts +++ b/packages/jest-source-map/src/getCallsite.ts @@ -9,6 +9,8 @@ import {readFileSync} from 'graceful-fs'; import callsites = require('callsites'); import {SourceMapConsumer} from 'source-map'; import type {SourceMapRegistry} from './types'; +import StackUtils = require('stack-utils'); +import {ErrorWithStack} from 'jest-util'; // Copied from https://github.com/rexxars/sourcemap-decorate-callsites/blob/5b9735a156964973a75dc62fd2c7f0c1975458e8/lib/index.js#L113-L158 const addSourceMapConsumer = ( @@ -66,3 +68,42 @@ export default ( return stack; }; + +export const getSourceMappedStack = ( + level: number, + sourceMaps ?: SourceMapRegistry | null, +): string => { + + const rawStack = new ErrorWithStack( + undefined, + getSourceMappedStack) + .stack || ''; + + const stackUtils = new StackUtils({ + cwd: 'something which does not exist', + wrapCallSite: (callsite: StackUtils.CallSite) => { + const sourceMapFileName = sourceMaps && + sourceMaps[callsite.getFileName() || '']; + + if (sourceMapFileName) { + try { + const sourceMap = readFileSync(sourceMapFileName, 'utf8'); + // @ts-ignore: Not allowed to pass string + addSourceMapConsumer(stack, new SourceMapConsumer(sourceMap)); + } catch (e) { + // ignore + } + } + return callsite; + } + }); + + const sourceMapped = stackUtils.clean(rawStack) + .split('\n') + .slice(level) + .filter(Boolean) + .map(s=>'\tat '+s) + .join('\n'); + + return sourceMapped; +}; \ No newline at end of file diff --git a/packages/jest-source-map/src/index.ts b/packages/jest-source-map/src/index.ts index 37a0bc1d525b..12bd704f1b4c 100644 --- a/packages/jest-source-map/src/index.ts +++ b/packages/jest-source-map/src/index.ts @@ -5,5 +5,5 @@ * LICENSE file in the root directory of this source tree. */ -export {default as getCallsite} from './getCallsite'; +export {default as getCallsite, getSourceMappedStack} from './getCallsite'; export type {SourceMapRegistry} from './types';