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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
57 changes: 57 additions & 0 deletions 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 {LogType} from '../types';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import {LogType} from '../types';
import type {LogType} from '../types';


jest.mock('jest-message-util', () => ({
formatStackTrace: jest.fn(),
}));

describe('getConsoleOutput', () => {
formatStackTrace.mockImplementation(() => 'throw new Error("Whoops!");');
const cases = [
'assert',
'count',
'debug',
'dir',
'dirxml',
'error',
'group',
'groupCollapsed',
'info',
'log',
'time',
'warn',
];

cases.forEach(logType => {
Copy link
Member

Choose a reason for hiding this comment

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

it.each?

it(`takes noStackTrace and pass it on for ${logType}`, () => {
getConsoleOutput(
'someRootPath',
true,
BufferedConsole.write([], logType as LogType, 'message', 4),
{
rootDir: 'root',
testMatch: [],
},
true,
);
expect(formatStackTrace).toHaveBeenCalled();
expect(formatStackTrace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining({
noCodeFrame: expect.anything(),
noStackTrace: true,
}),
);
});
});
});
11 changes: 7 additions & 4 deletions packages/jest-console/src/getConsoleOutput.ts
Expand Up @@ -11,18 +11,21 @@ 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: [],
},
globalConfig: Config.GlobalConfig,
Copy link
Member

Choose a reason for hiding this comment

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

Needs to be optional until next major ☹️ Should create an issue we can add to the Jest 27 milestone so we don't forget (again)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created this one and can create a PR for it, please let me know if anything is missing.

Copy link
Member

Choose a reason for hiding this comment

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

Perfect 👍

): string => {
const TITLE_INDENT = verbose ? ' ' : ' ';
const CONSOLE_INDENT = TITLE_INDENT + ' ';
Expand All @@ -40,12 +43,12 @@ export default (
if (type === 'warn') {
message = chalk.yellow(message);
typeMessage = chalk.yellow(typeMessage);
noStackTrace = false;
noStackTrace = false || globalConfig.noStackTrace;
Copy link
Member

Choose a reason for hiding this comment

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

false || will always fallback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching! updated in a following commit.

noCodeFrame = false;
} else if (type === 'error') {
message = chalk.red(message);
typeMessage = chalk.red(typeMessage);
noStackTrace = false;
noStackTrace = false || globalConfig.noStackTrace;
noCodeFrame = false;
}

Expand Down
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
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
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
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