From 46c4cd01b98378ba91fb98fb337120a3c9624c46 Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Wed, 22 Apr 2020 22:19:39 +0200 Subject: [PATCH 1/6] noCodeFrame respects noStackTrace --- .../__snapshots__/messages.test.ts.snap | 21 +++++ .../src/__tests__/messages.test.ts | 78 ++++++++++++++++++- packages/jest-message-util/src/index.ts | 3 +- 3 files changed, 99 insertions(+), 3 deletions(-) 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 490db062e6fb..99256df0436c 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 @@ -35,6 +35,27 @@ exports[`formatStackTrace should strip node internals 1`] = ` " `; +exports[`getConsoleOutput 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`] = ` +" + at Object. (file.js:1:7) + " +`; + +exports[`getConsoleOutput prints code frame and stacktrace 1`] = ` +" + > 1 | throw new Error(\\"Whoops!\\"); + | ^ + + at Object. (file.js:1:7) + " +`; + 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 41a14d0100da..c0ed7dcc9cf2 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -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(); @@ -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. (${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. (${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. (${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(); + }); +}); diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index b3c1fbb5967b..ce4ba5c240f9 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -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; From 03a9297de91443dee1124d39ac181ea326f649fe Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Wed, 22 Apr 2020 22:31:29 +0200 Subject: [PATCH 2/6] Add change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f1165f46273..447dbb7c8139 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[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-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866)) ### Chore & Maintenance From 4873bd226ac839e72bc94fb670bf22b50018fa71 Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Wed, 22 Apr 2020 22:31:35 +0200 Subject: [PATCH 3/6] linting --- .../jest-message-util/src/__tests__/messages.test.ts | 12 ++++++------ packages/jest-message-util/src/index.ts | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index c0ed7dcc9cf2..e4b3d3bf8dad 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('getConsoleOutput', () => { it('prints code frame and stacktrace', () => { readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");'); const message = formatStackTrace( @@ -312,10 +312,10 @@ describe('getConsoleOutput', () =>{ }, 'path_test', ); - + expect(message).toMatchSnapshot(); }); - + it('does not print code frame when noCodeFrame = true', () => { readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");'); const message = formatStackTrace( @@ -337,10 +337,10 @@ describe('getConsoleOutput', () =>{ }, 'path_test', ); - + expect(message).toMatchSnapshot(); }); - + it('does not print codeframe when noStackTrace = true', () => { readFileSync.mockImplementationOnce(() => 'throw new Error("Whoops!");'); const message = formatStackTrace( @@ -361,7 +361,7 @@ describe('getConsoleOutput', () =>{ }, 'path_test', ); - + expect(message).toMatchSnapshot(); }); }); diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index ce4ba5c240f9..e205c823d648 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -283,7 +283,7 @@ export const formatStackTrace = ( ? slash(path.relative(config.rootDir, testPath)) : null; - if ((!options.noStackTrace) && (!options.noCodeFrame)) { + if (!options.noStackTrace && !options.noCodeFrame) { const topFrame = getTopFrame(lines); if (topFrame) { const {column, file: filename, line} = topFrame; From 216971c48376522180cf63c2f8cc8c528a35baa4 Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Thu, 23 Apr 2020 21:36:32 +0200 Subject: [PATCH 4/6] wrap snapshot --- .../__snapshots__/messages.test.ts.snap | 22 +++++++++---------- .../src/__tests__/messages.test.ts | 7 +++--- 2 files changed, 15 insertions(+), 14 deletions(-) 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 99256df0436c..864ec781fc8a 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 @@ -36,24 +36,24 @@ exports[`formatStackTrace should strip node internals 1`] = ` `; exports[`getConsoleOutput does not print code frame when noCodeFrame = true 1`] = ` -" - at Object. (file.js:1:7) - " + + at Object. (file.js:1:7) + `; exports[`getConsoleOutput does not print codeframe when noStackTrace = true 1`] = ` -" - at Object. (file.js:1:7) - " + + at Object. (file.js:1:7) + `; exports[`getConsoleOutput prints code frame and stacktrace 1`] = ` -" - > 1 | throw new Error(\\"Whoops!\\"); - | ^ - at Object. (file.js:1:7) - " + > 1 | throw new Error("Whoops!"); +   |  ^ + + at Object. (file.js:1:7) + `; exports[`no codeframe 1`] = ` diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index e4b3d3bf8dad..de463d93dffe 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -9,6 +9,7 @@ import {readFileSync} from 'fs'; import slash = require('slash'); import tempy = require('tempy'); +import {wrap} from 'jest-snapshot-serializer-raw'; import {formatExecError, formatResultsErrors, formatStackTrace} from '..'; const rootDir = tempy.directory(); @@ -313,7 +314,7 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(message).toMatchSnapshot(); + expect(wrap(message)).toMatchSnapshot(); }); it('does not print code frame when noCodeFrame = true', () => { @@ -338,7 +339,7 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(message).toMatchSnapshot(); + expect(wrap(message)).toMatchSnapshot(); }); it('does not print codeframe when noStackTrace = true', () => { @@ -362,6 +363,6 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(message).toMatchSnapshot(); + expect(wrap(message)).toMatchSnapshot(); }); }); From aa553ed82a07588f20bac2a505d5a11928a76ad1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 22:23:06 +0200 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 447dbb7c8139..1fdca416b91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ ### 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-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866)) ### Chore & Maintenance From 69c74e1a7c7f0b688661ab878749d10b3e52d634 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 22:23:49 +0200 Subject: [PATCH 6/6] Revert "wrap snapshot" This reverts commit 216971c48376522180cf63c2f8cc8c528a35baa4. --- .../__snapshots__/messages.test.ts.snap | 22 +++++++++---------- .../src/__tests__/messages.test.ts | 7 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) 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 864ec781fc8a..99256df0436c 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 @@ -36,24 +36,24 @@ exports[`formatStackTrace should strip node internals 1`] = ` `; exports[`getConsoleOutput does not print code frame when noCodeFrame = true 1`] = ` - - at Object. (file.js:1:7) - +" + at Object. (file.js:1:7) + " `; exports[`getConsoleOutput does not print codeframe when noStackTrace = true 1`] = ` - - at Object. (file.js:1:7) - +" + at Object. (file.js:1:7) + " `; exports[`getConsoleOutput prints code frame and stacktrace 1`] = ` +" + > 1 | throw new Error(\\"Whoops!\\"); + | ^ - > 1 | throw new Error("Whoops!"); -   |  ^ - - at Object. (file.js:1:7) - + at Object. (file.js:1:7) + " `; exports[`no codeframe 1`] = ` diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index de463d93dffe..e4b3d3bf8dad 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -9,7 +9,6 @@ import {readFileSync} from 'fs'; import slash = require('slash'); import tempy = require('tempy'); -import {wrap} from 'jest-snapshot-serializer-raw'; import {formatExecError, formatResultsErrors, formatStackTrace} from '..'; const rootDir = tempy.directory(); @@ -314,7 +313,7 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(wrap(message)).toMatchSnapshot(); + expect(message).toMatchSnapshot(); }); it('does not print code frame when noCodeFrame = true', () => { @@ -339,7 +338,7 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(wrap(message)).toMatchSnapshot(); + expect(message).toMatchSnapshot(); }); it('does not print codeframe when noStackTrace = true', () => { @@ -363,6 +362,6 @@ describe('getConsoleOutput', () => { 'path_test', ); - expect(wrap(message)).toMatchSnapshot(); + expect(message).toMatchSnapshot(); }); });