From 7ca500c95e5d9b9ea3dfd80897a4a4d836ff528a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Mar 2018 13:02:40 +0200 Subject: [PATCH 1/4] include columns in stack frames --- CHANGELOG.md | 2 ++ .../custom_matcher_stack_trace.test.js.snap | 1 + .../__snapshots__/failures.test.js.snap | 19 +++++++++++++++++++ .../__snapshots__/globals.test.js.snap | 2 ++ packages/jest-message-util/src/index.js | 14 +++++++++++--- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47daaf340e1a..7a1b32080862 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ mode. ([#5861](https://github.com/facebook/jest/pull/5861)) * `[jest-mock]` Extend .toHaveBeenCalled return message with outcome ([#5951](https://github.com/facebook/jest/pull/5951)) +* `[jest-message-util]` Include column in stack frames + (TBD) * `[jest-runner]` Assign `process.env.JEST_WORKER_ID="1"` when in runInBand mode ([#5860](https://github.com/facebook/jest/pull/5860)) * `[jest-cli]` Add descriptive error message when trying to use diff --git a/integration-tests/__tests__/__snapshots__/custom_matcher_stack_trace.test.js.snap b/integration-tests/__tests__/__snapshots__/custom_matcher_stack_trace.test.js.snap index 5e6181192e26..181e4d0c3df7 100644 --- a/integration-tests/__tests__/__snapshots__/custom_matcher_stack_trace.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/custom_matcher_stack_trace.test.js.snap @@ -14,6 +14,7 @@ exports[`works with custom matchers 1`] = ` 43 | const bar = () => baz(); 44 | const baz = () => { > 45 | throw Error('qux'); + | ^ 46 | }; 47 | 48 | // This expecation fails due to an error we throw (intentionally) diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index fb24bb8f8a3f..882190669688 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -42,6 +42,7 @@ exports[`not throwing Error objects 4`] = ` 11 | const throws = () => { 12 | expect.assertions(2); > 13 | expect(false).toBeTruthy(); + | ^ 14 | }; 15 | const redeclare = () => { 16 | expect.assertions(1); @@ -55,6 +56,7 @@ exports[`not throwing Error objects 4`] = ` 15 | const redeclare = () => { 16 | expect.assertions(1); > 17 | expect(false).toBeTruthy(); + | ^ 18 | expect.assertions(2); 19 | }; 20 | @@ -174,6 +176,7 @@ exports[`works with assertions in separate files 1`] = ` 10 | 11 | module.exports = (one: any, two: any) => { > 12 | expect(one).toEqual(two); + | ^ 13 | }; 14 | @@ -242,6 +245,7 @@ exports[`works with node assert 1`] = ` 13 | 14 | test('assert', () => { > 15 | assert(false); + | ^ 16 | }); 17 | 18 | test('assert with a message', () => { @@ -263,6 +267,7 @@ exports[`works with node assert 1`] = ` 17 | 18 | test('assert with a message', () => { > 19 | assert(false, 'this is a message'); + | ^ 20 | }); 21 | 22 | test('assert.ok', () => { @@ -281,6 +286,7 @@ exports[`works with node assert 1`] = ` 21 | 22 | test('assert.ok', () => { > 23 | assert.ok(false); + | ^ 24 | }); 25 | 26 | test('assert.ok with a message', () => { @@ -302,6 +308,7 @@ exports[`works with node assert 1`] = ` 25 | 26 | test('assert.ok with a message', () => { > 27 | assert.ok(false, 'this is a message'); + | ^ 28 | }); 29 | 30 | test('assert.equal', () => { @@ -320,6 +327,7 @@ exports[`works with node assert 1`] = ` 29 | 30 | test('assert.equal', () => { > 31 | assert.equal(1, 2); + | ^ 32 | }); 33 | 34 | test('assert.notEqual', () => { @@ -342,6 +350,7 @@ exports[`works with node assert 1`] = ` 33 | 34 | test('assert.notEqual', () => { > 35 | assert.notEqual(1, 1); + | ^ 36 | }); 37 | 38 | test('assert.deepEqual', () => { @@ -374,6 +383,7 @@ exports[`works with node assert 1`] = ` 37 | 38 | test('assert.deepEqual', () => { > 39 | assert.deepEqual({a: {b: {c: 5}}}, {a: {b: {c: 6}}}); + | ^ 40 | }); 41 | 42 | test('assert.deepEqual with a message', () => { @@ -409,6 +419,7 @@ exports[`works with node assert 1`] = ` 41 | 42 | test('assert.deepEqual with a message', () => { > 43 | assert.deepEqual({a: {b: {c: 5}}}, {a: {b: {c: 7}}}, 'this is a message'); + | ^ 44 | }); 45 | 46 | test('assert.notDeepEqual', () => { @@ -431,6 +442,7 @@ exports[`works with node assert 1`] = ` 45 | 46 | test('assert.notDeepEqual', () => { > 47 | assert.notDeepEqual({a: 1}, {a: 1}); + | ^ 48 | }); 49 | 50 | test('assert.strictEqual', () => { @@ -449,6 +461,7 @@ exports[`works with node assert 1`] = ` 49 | 50 | test('assert.strictEqual', () => { > 51 | assert.strictEqual(1, NaN); + | ^ 52 | }); 53 | 54 | test('assert.notStrictEqual', () => { @@ -474,6 +487,7 @@ exports[`works with node assert 1`] = ` 53 | 54 | test('assert.notStrictEqual', () => { > 55 | assert.notStrictEqual(1, 1, 'My custom error message'); + | ^ 56 | }); 57 | 58 | test('assert.deepStrictEqual', () => { @@ -502,6 +516,7 @@ exports[`works with node assert 1`] = ` 57 | 58 | test('assert.deepStrictEqual', () => { > 59 | assert.deepStrictEqual({a: 1}, {a: 2}); + | ^ 60 | }); 61 | 62 | test('assert.notDeepStrictEqual', () => { @@ -524,6 +539,7 @@ exports[`works with node assert 1`] = ` 61 | 62 | test('assert.notDeepStrictEqual', () => { > 63 | assert.notDeepStrictEqual({a: 1}, {a: 1}); + | ^ 64 | }); 65 | 66 | test('assert.ifError', () => { @@ -550,6 +566,7 @@ exports[`works with node assert 1`] = ` 69 | 70 | test('assert.doesNotThrow', () => { > 71 | assert.doesNotThrow(() => { + | ^ 72 | throw Error('err!'); 73 | }); 74 | }); @@ -569,6 +586,7 @@ exports[`works with node assert 1`] = ` 75 | 76 | test('assert.throws', () => { > 77 | assert.throws(() => {}); + | ^ 78 | }); 79 | @@ -593,6 +611,7 @@ exports[`works with snapshot failures 1`] = ` 10 | 11 | test('failing snapshot', () => { > 12 | expect('foo').toMatchSnapshot(); + | ^ 13 | }); 14 | diff --git a/integration-tests/__tests__/__snapshots__/globals.test.js.snap b/integration-tests/__tests__/__snapshots__/globals.test.js.snap index 90e81f980f56..56a97cf22209 100644 --- a/integration-tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/globals.test.js.snap @@ -29,6 +29,7 @@ exports[`cannot test with no implementation 1`] = ` 1 | 2 | it('it', () => {}); > 3 | it('it, no implementation'); + | ^ 4 | test('test, no implementation'); 5 | @@ -56,6 +57,7 @@ exports[`cannot test with no implementation with expand arg 1`] = ` 1 | 2 | it('it', () => {}); > 3 | it('it, no implementation'); + | ^ 4 | test('test, no implementation'); 5 | diff --git a/packages/jest-message-util/src/index.js b/packages/jest-message-util/src/index.js index a990cde4aa65..7b7767116a0b 100644 --- a/packages/jest-message-util/src/index.js +++ b/packages/jest-message-util/src/index.js @@ -73,10 +73,14 @@ const trim = string => (string || '').trim(); const trimPaths = string => string.match(STACK_PATH_REGEXP) ? trim(string) : string; -const getRenderedCallsite = (fileContent: string, line: number) => { +const getRenderedCallsite = ( + fileContent: string, + line: number, + column?: number, +) => { let renderedCallsite = codeFrameColumns( fileContent, - {start: {line}}, + {start: {column, line}}, {highlightCode: true}, ); @@ -258,7 +262,11 @@ export const formatStackTrace = ( // TODO: check & read HasteFS instead of reading the filesystem: // see: https://github.com/facebook/jest/pull/5405#discussion_r164281696 fileContent = fs.readFileSync(filename, 'utf8'); - renderedCallsite = getRenderedCallsite(fileContent, topFrame.line); + renderedCallsite = getRenderedCallsite( + fileContent, + topFrame.line, + topFrame.column, + ); } catch (e) { // the file does not exist or is inaccessible, we ignore } From 827cbeb3981c245b5964de59eeb91e215d02ee6c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Mar 2018 13:03:45 +0200 Subject: [PATCH 2/4] update changelog link --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1b32080862..d396d9c4ae5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,7 +70,7 @@ * `[jest-mock]` Extend .toHaveBeenCalled return message with outcome ([#5951](https://github.com/facebook/jest/pull/5951)) * `[jest-message-util]` Include column in stack frames - (TBD) + ([#5889](https://github.com/facebook/jest/pull/5889)) * `[jest-runner]` Assign `process.env.JEST_WORKER_ID="1"` when in runInBand mode ([#5860](https://github.com/facebook/jest/pull/5860)) * `[jest-cli]` Add descriptive error message when trying to use From 7ea1284f0ed71af15f557dfa33c04029657008fe Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 10 Apr 2018 09:16:24 +0200 Subject: [PATCH 3/4] fix node 9 test --- integration-tests/__tests__/failures.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/integration-tests/__tests__/failures.test.js b/integration-tests/__tests__/failures.test.js index ab9ad958b62b..e8fd6d0e65bf 100644 --- a/integration-tests/__tests__/failures.test.js +++ b/integration-tests/__tests__/failures.test.js @@ -78,6 +78,7 @@ test('works with node assert', () => { 69 | 70 | test('assert.doesNotThrow', () => { > 71 | assert.doesNotThrow(() => { + | ^ 72 | throw Error('err!'); 73 | }); 74 | }); From 6cd69c01839cfc3a7a1113bfcf88ac4666e27795 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 13 Apr 2018 03:54:21 +0200 Subject: [PATCH 4/4] update snapshots after rebase --- .../__tests__/__snapshots__/failures.test.js.snap | 3 +++ 1 file changed, 3 insertions(+) diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index 882190669688..e2c73766829e 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -118,6 +118,7 @@ exports[`not throwing Error objects 5`] = ` 26 | test('Error during test', () => { 27 | // eslint-disable-next-line no-undef > 28 | doesNotExist.alsoThisNot; + | ^ 29 | }); 30 | 31 | test('done(Error)', done => { @@ -131,6 +132,7 @@ exports[`not throwing Error objects 5`] = ` 30 | 31 | test('done(Error)', done => { > 32 | done(new Error('this is an error')); + | ^ 33 | }); 34 | 35 | test('done(non-error)', done => { @@ -151,6 +153,7 @@ exports[`not throwing Error objects 5`] = ` 34 | 35 | test('done(non-error)', done => { > 36 | done(deepObject); + | ^ 37 | }); 38 |