Skip to content

Commit

Permalink
noCodeFrame respects noStackTrace (#9866)
Browse files Browse the repository at this point in the history
  • Loading branch information
ychi committed Apr 23, 2020
1 parent 58d199d commit 6cbd3cb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
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))
- `[jest-transform]` Improve source map handling when instrumenting transformed code ([#9811](https://github.com/facebook/jest/pull/9811))
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();
});

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

0 comments on commit 6cbd3cb

Please sign in to comment.