Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getConsoleOutput receives global noStackTrace #10081

Merged
merged 11 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
72 changes: 72 additions & 0 deletions e2e/__tests__/__snapshots__/console.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,78 @@ Snapshots: 0 total
Time: <<REPLACED>>
`;

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: <<REPLACED>>
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: <<REPLACED>>
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`] = `
Expand Down
36 changes: 36 additions & 0 deletions e2e/__tests__/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
57 changes: 57 additions & 0 deletions packages/jest-console/src/__tests__/getConsoleOutput.test.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
);
});
});
12 changes: 8 additions & 4 deletions packages/jest-console/src/getConsoleOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed 26, I can follow closer and create PR when 27 is near.

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 + ' ';
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,19 @@ exports[`codeframe 1`] = `
"
`;

exports[`formatStackTrace should strip node internals 1`] = `
"<bold><red> <bold>● </><bold>Unix test</></>


Expected value to be of type:
\\"number\\"
Received:
\\"\\"
type:
\\"string\\"
<dim></>
<dim> <dim>at Object.it (</><dim>__tests__/test.js<dim>:8:14)</><dim></>
"
`;

exports[`getConsoleOutput does not print code frame when noCodeFrame = true 1`] = `
exports[`formatStackTrace does not print code frame when noCodeFrame = true 1`] = `
"
<dim>at Object.<anonymous> (</>file.js<dim>:1:7)</>
"
`;

exports[`getConsoleOutput does not print codeframe when noStackTrace = true 1`] = `
exports[`formatStackTrace does not print codeframe when noStackTrace = true 1`] = `
"
<dim>at Object.<anonymous> (</>file.js<dim>:1:7)</>
"
`;

exports[`getConsoleOutput prints code frame and stacktrace 1`] = `
exports[`formatStackTrace prints code frame and stacktrace 1`] = `
"
</><red><bold>></></><gray> 1 | </><cyan>throw</> <cyan>new</> <yellow>Error</>(<green>\\"Whoops!\\"</>)<yellow>;</></>
</> <gray> | </> <red><bold>^</></></>
Expand All @@ -55,6 +40,21 @@ exports[`getConsoleOutput prints code frame and stacktrace 1`] = `
"
`;

exports[`formatStackTrace should strip node internals 1`] = `
"<bold><red> <bold>● </><bold>Unix test</></>


Expected value to be of type:
\\"number\\"
Received:
\\"\\"
type:
\\"string\\"
<dim></>
<dim> <dim>at Object.it (</><dim>__tests__/test.js<dim>:8:14)</><dim></>
"
`;

exports[`no codeframe 1`] = `
" <bold>● </>Test suite failed to run

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-message-util/src/__tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default class DefaultReporter extends BaseReporter {
!!this._globalConfig.verbose,
result.console,
config,
this._globalConfig,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down