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

noCodeFrame respects noStackTrace #9866

Merged
merged 6 commits into from Apr 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@

### Fixes

- `[jest-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866))
- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
- `[jest-runtime]` Support importing parallel dynamic `import`s ([#9858](https://github.com/facebook/jest/pull/9858))

Expand Down
Expand Up @@ -35,6 +35,27 @@ exports[`formatStackTrace should strip node internals 1`] = `
"
`;

exports[`getConsoleOutput 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`] = `
"
<dim>at Object.<anonymous> (</>file.js<dim>:1:7)</>
"
`;

exports[`getConsoleOutput prints code frame and stacktrace 1`] = `
"
</><red><bold>></></><gray> 1 | </><cyan>throw</> <cyan>new</> <yellow>Error</>(<green>\\"Whoops!\\"</>)<yellow>;</></>
</> <gray> | </> <red><bold>^</></></>

<dim>at Object.<anonymous> (</>file.js<dim>:1:7)</>
"
`;

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

Expand Down
78 changes: 77 additions & 1 deletion packages/jest-message-util/src/__tests__/messages.test.ts
Expand Up @@ -9,7 +9,7 @@
import {readFileSync} from 'fs';
import slash = require('slash');
import tempy = require('tempy');
import {formatExecError, formatResultsErrors} from '..';
import {formatExecError, formatResultsErrors, formatStackTrace} from '..';

const rootDir = tempy.directory();

Expand Down Expand Up @@ -289,3 +289,79 @@ it('no stack', () => {

expect(message).toMatchSnapshot();
});

describe('getConsoleOutput', () => {
it('prints code frame and stacktrace', () => {
readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");');
const message = formatStackTrace(
`
at Object.<anonymous> (${slash(rootDir)}/file.js:1:7)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
`,
{
rootDir,
testMatch: [],
},
{
noCodeFrame: false,
noStackTrace: false,
},
'path_test',
);

expect(message).toMatchSnapshot();
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
expect(message).toMatchSnapshot();
expect(wrap(message)).toMatchSnapshot();

import {wrap} from 'jest-snapshot-serializer-raw';

will get rid of the wrapping "`.

Please apply to both 😀

});

it('does not print code frame when noCodeFrame = true', () => {
readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");');
const message = formatStackTrace(
`
at Object.<anonymous> (${slash(rootDir)}/file.js:1:7)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
`,
{
rootDir,
testMatch: [],
},
{
noCodeFrame: true,
noStackTrace: false,
},
'path_test',
);

expect(message).toMatchSnapshot();
});

it('does not print codeframe when noStackTrace = true', () => {
readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");');
const message = formatStackTrace(
`
at Object.<anonymous> (${slash(rootDir)}/file.js:1:7)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
`,
{
rootDir,
testMatch: [],
},
{
noStackTrace: true,
},
'path_test',
);

expect(message).toMatchSnapshot();
});
});
3 changes: 1 addition & 2 deletions packages/jest-message-util/src/index.ts
Expand Up @@ -283,9 +283,8 @@ export const formatStackTrace = (
? slash(path.relative(config.rootDir, testPath))
: null;

if (!options.noCodeFrame) {
if (!options.noStackTrace && !options.noCodeFrame) {
const topFrame = getTopFrame(lines);

if (topFrame) {
const {column, file: filename, line} = topFrame;

Expand Down