diff --git a/CHANGELOG.md b/CHANGELOG.md index 629bb31c87cb..85c66a5d976f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- `[jest-console]` `getConsoleOutput` to receive global stack trace config and use it to format stack trace ([#10081](https://github.com/facebook/jest/pull/10081)) - `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990)) - `[jest-diff]` Control no diff message color with `commonColor` in diff options ([#9997](https://github.com/facebook/jest/pull/9997)) - `[jest-snapshot]` Fix TypeScript compilation ([#10008](https://github.com/facebook/jest/pull/10008)) diff --git a/e2e/__tests__/__snapshots__/console.test.ts.snap b/e2e/__tests__/__snapshots__/console.test.ts.snap index 161e2e9e4c21..d49da1630922 100644 --- a/e2e/__tests__/__snapshots__/console.test.ts.snap +++ b/e2e/__tests__/__snapshots__/console.test.ts.snap @@ -145,6 +145,78 @@ Snapshots: 0 total Time: <> `; +exports[`respects --noStackTrace 1`] = ` + console.log + This is a log message. + + at Object.log (__tests__/console.test.js:10:11) + + console.info + This is an info message. + + at Object.info (__tests__/console.test.js:12:11) + + console.warn + This is a warning message. + + at Object.warn (__tests__/console.test.js:14:11) + + console.error + This is an error message. + + at Object.error (__tests__/console.test.js:16:11) + +`; + +exports[`respects --noStackTrace 2`] = ` +PASS __tests__/console.test.js + ✓ works just fine +`; + +exports[`respects --noStackTrace 3`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; + +exports[`respects noStackTrace in config 1`] = ` + console.log + This is a log message. + + at Object.log (__tests__/console.test.js:10:11) + + console.info + This is an info message. + + at Object.info (__tests__/console.test.js:12:11) + + console.warn + This is a warning message. + + at Object.warn (__tests__/console.test.js:14:11) + + console.error + This is an error message. + + at Object.error (__tests__/console.test.js:16:11) + +`; + +exports[`respects noStackTrace in config 2`] = ` +PASS __tests__/console.test.js + ✓ works just fine +`; + +exports[`respects noStackTrace in config 3`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; + exports[`the jsdom console is the same as the test console 1`] = ``; exports[`the jsdom console is the same as the test console 2`] = ` diff --git a/e2e/__tests__/console.test.ts b/e2e/__tests__/console.test.ts index 7c78ae3c1bf6..33bfa423c193 100644 --- a/e2e/__tests__/console.test.ts +++ b/e2e/__tests__/console.test.ts @@ -50,6 +50,42 @@ test('does not print to console with --silent', () => { expect(wrap(summary)).toMatchSnapshot(); }); +test('respects --noStackTrace', () => { + const {stderr, stdout, exitCode} = runJest('console', [ + // Need to pass --config because console test specifies `verbose: false` + '--config=' + + JSON.stringify({ + testEnvironment: 'node', + }), + '--noStackTrace', + '--no-cache', + ]); + const {summary, rest} = extractSummary(stderr); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); +}); + +test('respects noStackTrace in config', () => { + const {stderr, stdout, exitCode} = runJest('console', [ + // Need to pass --config because console test specifies `verbose: false` + '--config=' + + JSON.stringify({ + noStackTrace: true, + testEnvironment: 'node', + }), + '--no-cache', + ]); + const {summary, rest} = extractSummary(stderr); + + expect(exitCode).toBe(0); + expect(wrap(stdout)).toMatchSnapshot(); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); +}); + // issue: https://github.com/facebook/jest/issues/5223 test('the jsdom console is the same as the test console', () => { const {stderr, stdout, exitCode} = runJest('console-jsdom'); diff --git a/packages/jest-console/src/__tests__/getConsoleOutput.test.ts b/packages/jest-console/src/__tests__/getConsoleOutput.test.ts new file mode 100644 index 000000000000..fc0a2749a49f --- /dev/null +++ b/packages/jest-console/src/__tests__/getConsoleOutput.test.ts @@ -0,0 +1,57 @@ +/** + * 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. + */ + +import {formatStackTrace} from 'jest-message-util'; +import getConsoleOutput from '../getConsoleOutput'; +import BufferedConsole from '../BufferedConsole'; +import type {LogType} from '../types'; +import {makeGlobalConfig} from '../../../../TestUtils'; + +jest.mock('jest-message-util', () => ({ + formatStackTrace: jest.fn(), +})); + +describe('getConsoleOutput', () => { + const globalConfig = makeGlobalConfig({noStackTrace: true}); + formatStackTrace.mockImplementation(() => 'throw new Error("Whoops!");'); + + it.each` + logType + ${'assert'} + ${'count'} + ${'debug'} + ${'dir'} + ${'dirxml'} + ${'error'} + ${'group'} + ${'groupCollapsed'} + ${'info'} + ${'log'} + ${'time'} + ${'warn'} + `('takes noStackTrace and pass it on for $logType', logType => { + getConsoleOutput( + 'someRootPath', + true, + BufferedConsole.write([], logType as LogType, 'message', 4), + { + rootDir: 'root', + testMatch: [], + }, + globalConfig, + ); + expect(formatStackTrace).toHaveBeenCalled(); + expect(formatStackTrace).toHaveBeenCalledWith( + expect.anything(), + expect.anything(), + expect.objectContaining({ + noCodeFrame: expect.anything(), + noStackTrace: true, + }), + ); + }); +}); diff --git a/packages/jest-console/src/getConsoleOutput.ts b/packages/jest-console/src/getConsoleOutput.ts index 1c3dba19f898..f6f0369f50ee 100644 --- a/packages/jest-console/src/getConsoleOutput.ts +++ b/packages/jest-console/src/getConsoleOutput.ts @@ -11,18 +11,22 @@ import { StackTraceOptions, formatStackTrace, } from 'jest-message-util'; +import type {Config} from '@jest/types'; import type {ConsoleBuffer} from './types'; export default ( - // TODO: remove in 26 + // TODO: remove in 27 root: string, + // TODO: this is covered by GlobalConfig, switch over in 27 verbose: boolean, buffer: ConsoleBuffer, - // TODO: make mandatory and take Config.ProjectConfig in 26 + // TODO: make mandatory and take Config.ProjectConfig in 27 config: StackTraceConfig = { rootDir: root, testMatch: [], }, + // TODO: make mandatory in 27 + globalConfig?: Config.GlobalConfig, ): string => { const TITLE_INDENT = verbose ? ' ' : ' '; const CONSOLE_INDENT = TITLE_INDENT + ' '; @@ -40,12 +44,12 @@ export default ( if (type === 'warn') { message = chalk.yellow(message); typeMessage = chalk.yellow(typeMessage); - noStackTrace = false; + noStackTrace = globalConfig?.noStackTrace ?? false; noCodeFrame = false; } else if (type === 'error') { message = chalk.red(message); typeMessage = chalk.red(typeMessage); - noStackTrace = false; + noStackTrace = globalConfig?.noStackTrace ?? false; noCodeFrame = false; } diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap index ccce82f85b46..e6e03daebeee 100644 --- a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap +++ b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap @@ -19,34 +19,19 @@ exports[`codeframe 1`] = ` " `; -exports[`formatStackTrace should strip node internals 1`] = ` -" Unix test - - - Expected value to be of type: - \\"number\\" - Received: - \\"\\" - type: - \\"string\\" - - at Object.it (__tests__/test.js:8:14) -" -`; - -exports[`getConsoleOutput does not print code frame when noCodeFrame = true 1`] = ` +exports[`formatStackTrace does not print code frame when noCodeFrame = true 1`] = ` " at Object. (file.js:1:7) " `; -exports[`getConsoleOutput does not print codeframe when noStackTrace = true 1`] = ` +exports[`formatStackTrace does not print codeframe when noStackTrace = true 1`] = ` " at Object. (file.js:1:7) " `; -exports[`getConsoleOutput prints code frame and stacktrace 1`] = ` +exports[`formatStackTrace prints code frame and stacktrace 1`] = ` " > 1 | throw new Error(\\"Whoops!\\"); | ^ @@ -55,6 +40,21 @@ exports[`getConsoleOutput prints code frame and stacktrace 1`] = ` " `; +exports[`formatStackTrace should strip node internals 1`] = ` +" Unix test + + + Expected value to be of type: + \\"number\\" + Received: + \\"\\" + type: + \\"string\\" + + at Object.it (__tests__/test.js:8:14) +" +`; + exports[`no codeframe 1`] = ` " ● Test suite failed to run diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index 55c24797cb58..cb4d7e91f959 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -290,7 +290,7 @@ it('no stack', () => { expect(message).toMatchSnapshot(); }); -describe('getConsoleOutput', () => { +describe('formatStackTrace', () => { it('prints code frame and stacktrace', () => { readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");'); const message = formatStackTrace( diff --git a/packages/jest-reporters/src/default_reporter.ts b/packages/jest-reporters/src/default_reporter.ts index 9053fbf20eac..4f0046b2111f 100644 --- a/packages/jest-reporters/src/default_reporter.ts +++ b/packages/jest-reporters/src/default_reporter.ts @@ -183,6 +183,7 @@ export default class DefaultReporter extends BaseReporter { !!this._globalConfig.verbose, result.console, config, + this._globalConfig, ), ); } diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index deb20ff7d7ec..bc73db185c42 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -121,6 +121,7 @@ async function runTestInternal( // 4 = the console call is buried 4 stack frames deep BufferedConsole.write([], type, message, 4), config, + globalConfig, ); let testConsole;