From 6fe69d1467c329f872ab85b7325e7908796b35d0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 May 2018 07:57:08 +0200 Subject: [PATCH] chore: avoid adding trailing whitespace to blank lines in stack traces (#6211) * chore: avoid adding trailing whitespace to blank lines * link to PR * trim for node 9 and 10 as well * remove unecessary double stack when we have one * trim moar * trim when adding custom stack trace * update other snapshots --- CHANGELOG.md | 2 + .../custom_matcher_stack_trace.test.js.snap | 2 +- .../__snapshots__/detect_open_handles.js.snap | 2 +- .../__tests__/__snapshots__/each.test.js.snap | 6 +- .../expect-async-matcher.test.js.snap | 6 +- .../__snapshots__/failures.test.js.snap | 225 ++++++++---------- .../module_name_mapper.test.js.snap | 6 +- integration-tests/__tests__/failures.test.js | 21 +- .../to_throw_matchers.test.js.snap | 32 +-- .../src/expectation_result_factory.js | 12 +- .../__snapshots__/messages.test.js.snap | 10 +- packages/jest-message-util/src/index.js | 25 +- 12 files changed, 159 insertions(+), 190 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a9c529d8b82..c7b55999a023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -191,6 +191,8 @@ ([#5812](https://github.com/facebook/jest/issues/5812)) * `[jest-runtime]` [**BREAKING**] Remove `jest.genMockFn` and `jest.genMockFunction` ([#6173](https://github.com/facebook/jest/pull/6173)) +* `[jest-message-util]` Avoid adding unnecessary indent to blank lines in stack + traces ([#6211](https://github.com/facebook/jest/pull/6211)) ## 22.4.2 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 181e4d0c3df7..ea8c3dc6a7b7 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 @@ -18,7 +18,7 @@ exports[`works with custom matchers 1`] = ` 46 | }; 47 | 48 | // This expecation fails due to an error we throw (intentionally) - + at __tests__/custom_matcher.test.js:45:13 at __tests__/custom_matcher.test.js:43:23 at __tests__/custom_matcher.test.js:42:23 diff --git a/integration-tests/__tests__/__snapshots__/detect_open_handles.js.snap b/integration-tests/__tests__/__snapshots__/detect_open_handles.js.snap index 824eab153450..046161397d47 100644 --- a/integration-tests/__tests__/__snapshots__/detect_open_handles.js.snap +++ b/integration-tests/__tests__/__snapshots__/detect_open_handles.js.snap @@ -22,7 +22,7 @@ exports[`prints out info about open handlers 1`] = ` > 7 | app.listen({host: 'localhost', port: 0}); | ^ 8 | - + at Object. (server.js:7:5) at Object. (__tests__/test.js:3:1)" `; diff --git a/integration-tests/__tests__/__snapshots__/each.test.js.snap b/integration-tests/__tests__/__snapshots__/each.test.js.snap index f76a1b92fe86..40bc48c72317 100644 --- a/integration-tests/__tests__/__snapshots__/each.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/each.test.js.snap @@ -8,16 +8,16 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] Not enough arguments supplied for given headings: left | right - + Received: Array [ true, true, true, ] - + Missing 1 arguments - + at packages/jest-jasmine2/build/each.js:84:17 " diff --git a/integration-tests/__tests__/__snapshots__/expect-async-matcher.test.js.snap b/integration-tests/__tests__/__snapshots__/expect-async-matcher.test.js.snap index 3076bd3d1d5d..2b179d34966a 100644 --- a/integration-tests/__tests__/__snapshots__/expect-async-matcher.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/expect-async-matcher.test.js.snap @@ -10,6 +10,7 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` ● fail with expected non promise values Error + Error: Expected value to have length: 2 Received: @@ -20,6 +21,7 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` ● fail with expected non promise values and not Error + Error: Expected value to not have length: 2 Received: @@ -43,7 +45,7 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` 25 | Promise.resolve(2) 26 | ); 27 | }); - + at __tests__/failure.test.js:24:54 at __tests__/failure.test.js:11:191 at __tests__/failure.test.js:11:437 @@ -65,7 +67,7 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` 31 | Promise.resolve(2) 32 | ); 33 | }); - + at __tests__/failure.test.js:30:61 at __tests__/failure.test.js:11:191 at __tests__/failure.test.js:11:437 diff --git a/integration-tests/__tests__/__snapshots__/failures.test.js.snap b/integration-tests/__tests__/__snapshots__/failures.test.js.snap index d2bedc54ac4b..c5b082ab0170 100644 --- a/integration-tests/__tests__/__snapshots__/failures.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/failures.test.js.snap @@ -14,6 +14,7 @@ exports[`not throwing Error objects 2`] = ` ● Test suite failed to run Error + banana " @@ -40,7 +41,7 @@ exports[`not throwing Error objects 4`] = ` ● .assertions() › throws expect(received).toBeTruthy() - + Received: false 11 | const throws = () => { @@ -50,13 +51,13 @@ exports[`not throwing Error objects 4`] = ` 14 | }; 15 | const redeclare = () => { 16 | expect.assertions(1); - + at __tests__/assertion_count.test.js:13:17 ● .assertions() › throws expect.assertions(2) - + Expected two assertions to be called but received one assertion call. 10 | @@ -66,13 +67,13 @@ exports[`not throwing Error objects 4`] = ` 13 | expect(false).toBeTruthy(); 14 | }; 15 | const redeclare = () => { - + at __tests__/assertion_count.test.js:12:10 ● .assertions() › throws on redeclare of assertion count expect(received).toBeTruthy() - + Received: false 15 | const redeclare = () => { @@ -82,13 +83,13 @@ exports[`not throwing Error objects 4`] = ` 18 | expect.assertions(2); 19 | }; 20 | - + at __tests__/assertion_count.test.js:17:17 ● .assertions() › throws on assertion expect.assertions(0) - + Expected zero assertions to be called but received one assertion call. 20 | @@ -98,13 +99,13 @@ exports[`not throwing Error objects 4`] = ` 23 | expect(true).toBeTruthy(); 24 | }; 25 | - + at __tests__/assertion_count.test.js:22:10 ● .hasAssertions() › throws when there are not assertions expect.hasAssertions() - + Expected at least one assertion to be called but received none. 25 | @@ -114,7 +115,7 @@ exports[`not throwing Error objects 4`] = ` 28 | }; 29 | 30 | describe('.assertions()', () => { - + at __tests__/assertion_count.test.js:27:10 " @@ -141,8 +142,7 @@ exports[`not throwing Error objects 5`] = ` 8 | throw Promise.resolve(5); 9 | }); 10 | - - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/during_tests.test.js:7:1 @@ -157,8 +157,7 @@ exports[`not throwing Error objects 5`] = ` 12 | // eslint-disable-next-line no-throw-literal 13 | throw false; 14 | }); - - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/during_tests.test.js:11:1 @@ -173,8 +172,7 @@ exports[`not throwing Error objects 5`] = ` 17 | // eslint-disable-next-line no-throw-literal 18 | throw undefined; 19 | }); - - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/during_tests.test.js:16:1 @@ -196,8 +194,7 @@ exports[`not throwing Error objects 5`] = ` 22 | // eslint-disable-next-line no-throw-literal 23 | throw deepObject; 24 | }); - - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/during_tests.test.js:21:1 @@ -212,7 +209,7 @@ exports[`not throwing Error objects 5`] = ` 29 | }); 30 | 31 | test('done(Error)', done => { - + at __tests__/during_tests.test.js:28:3 ● done(Error) @@ -226,7 +223,7 @@ exports[`not throwing Error objects 5`] = ` 33 | }); 34 | 35 | test('done(non-error)', done => { - + at __tests__/during_tests.test.js:32:8 ● done(non-error) @@ -246,7 +243,7 @@ exports[`not throwing Error objects 5`] = ` | ^ 37 | }); 38 | - + at packages/jest-jasmine2/build/jasmine/Env.js:541:34 at __tests__/during_tests.test.js:36:3 @@ -260,7 +257,7 @@ exports[`works with assertions in separate files 1`] = ` ● use some imported macro to make assertion expect(received).toEqual(expected) - + Expected value to equal: 2 Received: @@ -272,7 +269,7 @@ exports[`works with assertions in separate files 1`] = ` | ^ 13 | }; 14 | - + at macros.js:12:15 at __tests__/test_macro.test.js:14:3 @@ -290,17 +287,17 @@ exports[`works with async failures 1`] = ` ● resolve, but fail expect(received).toEqual(expected) - + Expected value to equal: {\\"baz\\": \\"bar\\"} Received: {\\"foo\\": \\"bar\\"} - + Difference: - + - Expected + Received - + Object { - \\"baz\\": \\"bar\\", + \\"foo\\": \\"bar\\", @@ -313,23 +310,23 @@ exports[`works with async failures 1`] = ` 13 | }); 14 | 15 | test('reject, but fail', () => { - + at __tests__/async_failures.test.js:12:57 ● reject, but fail expect(received).toEqual(expected) - + Expected value to equal: {\\"baz\\": \\"bar\\"} Received: {\\"foo\\": \\"bar\\"} - + Difference: - + - Expected + Received - + Object { - \\"baz\\": \\"bar\\", + \\"foo\\": \\"bar\\", @@ -342,13 +339,13 @@ exports[`works with async failures 1`] = ` 17 | }); 18 | 19 | test('expect reject', () => { - + at __tests__/async_failures.test.js:16:55 ● expect reject expect(received).rejects.toEqual() - + Expected received Promise to reject, instead it resolved to value {\\"foo\\": \\"bar\\"} @@ -359,13 +356,13 @@ exports[`works with async failures 1`] = ` 21 | }); 22 | 23 | test('expect resolve', () => { - + at __tests__/async_failures.test.js:20:10 ● expect resolve expect(received).resolves.toEqual() - + Expected received Promise to resolve, instead it rejected to value {\\"foo\\": \\"bar\\"} @@ -376,7 +373,7 @@ exports[`works with async failures 1`] = ` 25 | }); 26 | 27 | test('timeout', done => { - + at __tests__/async_failures.test.js:24:10 ● timeout @@ -390,7 +387,7 @@ exports[`works with async failures 1`] = ` 28 | jest.setTimeout(5); 29 | 30 | setTimeout(done, 10); - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/async_failures.test.js:27:1 @@ -404,12 +401,12 @@ exports[`works with named snapshot failures 1`] = ` ● failing named snapshot expect(value).toMatchSnapshot() - + Received value does not match stored snapshot \\"failing named snapshot: snapname 1\\". - + - Snapshot + Received - + - \\"bar\\" + \\"foo\\" @@ -419,7 +416,7 @@ exports[`works with named snapshot failures 1`] = ` | ^ 13 | }); 14 | - + at __tests__/snapshot_named.test.js:12:17 › 1 snapshot failed. @@ -448,7 +445,7 @@ exports[`works with node assert 1`] = ` ● assert assert.equal(received, expected) or assert(received) - + Expected value to be equal to: true Received: @@ -461,20 +458,18 @@ exports[`works with node assert 1`] = ` 16 | }); 17 | 18 | test('assert with a message', () => { - + at __tests__/node_assertion_error.test.js:15:3 - - at __tests__/node_assertion_error.test.js:14:1 ● assert with a message assert.equal(received, expected) or assert(received) - + Expected value to be equal to: true Received: false - + Message: this is a message @@ -485,15 +480,13 @@ exports[`works with node assert 1`] = ` 20 | }); 21 | 22 | test('assert.ok', () => { - + at __tests__/node_assertion_error.test.js:19:3 - - at __tests__/node_assertion_error.test.js:18:1 ● assert.ok assert.equal(received, expected) or assert(received) - + Expected value to be equal to: true Received: @@ -506,20 +499,18 @@ exports[`works with node assert 1`] = ` 24 | }); 25 | 26 | test('assert.ok with a message', () => { - + at __tests__/node_assertion_error.test.js:23:10 - - at __tests__/node_assertion_error.test.js:22:1 ● assert.ok with a message assert.equal(received, expected) or assert(received) - + Expected value to be equal to: true Received: false - + Message: this is a message @@ -530,15 +521,13 @@ exports[`works with node assert 1`] = ` 28 | }); 29 | 30 | test('assert.equal', () => { - + at __tests__/node_assertion_error.test.js:27:10 - - at __tests__/node_assertion_error.test.js:26:1 ● assert.equal assert.equal(received, expected) or assert(received) - + Expected value to be equal to: 2 Received: @@ -551,22 +540,20 @@ exports[`works with node assert 1`] = ` 32 | }); 33 | 34 | test('assert.notEqual', () => { - + at __tests__/node_assertion_error.test.js:31:10 - - at __tests__/node_assertion_error.test.js:30:1 ● assert.notEqual assert.notEqual(received, expected) - + Expected value to not be equal to: 1 Received: 1 - + Difference: - + Compared values have no visual difference. 33 | @@ -576,25 +563,23 @@ exports[`works with node assert 1`] = ` 36 | }); 37 | 38 | test('assert.deepEqual', () => { - + at __tests__/node_assertion_error.test.js:35:10 - - at __tests__/node_assertion_error.test.js:34:1 ● assert.deepEqual assert.deepEqual(received, expected) - + Expected value to deeply equal to: {\\"a\\": {\\"b\\": {\\"c\\": 6}}} Received: {\\"a\\": {\\"b\\": {\\"c\\": 5}}} - + Difference: - + - Expected + Received - + Object { \\"a\\": Object { \\"b\\": Object { @@ -611,28 +596,26 @@ exports[`works with node assert 1`] = ` 40 | }); 41 | 42 | test('assert.deepEqual with a message', () => { - + at __tests__/node_assertion_error.test.js:39:10 - - at __tests__/node_assertion_error.test.js:38:1 ● assert.deepEqual with a message assert.deepEqual(received, expected) - + Expected value to deeply equal to: {\\"a\\": {\\"b\\": {\\"c\\": 7}}} Received: {\\"a\\": {\\"b\\": {\\"c\\": 5}}} - + Message: this is a message - + Difference: - + - Expected + Received - + Object { \\"a\\": Object { \\"b\\": Object { @@ -649,22 +632,20 @@ exports[`works with node assert 1`] = ` 44 | }); 45 | 46 | test('assert.notDeepEqual', () => { - + at __tests__/node_assertion_error.test.js:43:10 - - at __tests__/node_assertion_error.test.js:42:1 ● assert.notDeepEqual assert.notDeepEqual(received, expected) - + Expected value not to deeply equal to: {\\"a\\": 1} Received: {\\"a\\": 1} - + Difference: - + Compared values have no visual difference. 45 | @@ -674,15 +655,13 @@ exports[`works with node assert 1`] = ` 48 | }); 49 | 50 | test('assert.strictEqual', () => { - + at __tests__/node_assertion_error.test.js:47:10 - - at __tests__/node_assertion_error.test.js:46:1 ● assert.strictEqual assert.strictEqual(received, expected) - + Expected value to strictly be equal to: NaN Received: @@ -695,25 +674,23 @@ exports[`works with node assert 1`] = ` 52 | }); 53 | 54 | test('assert.notStrictEqual', () => { - + at __tests__/node_assertion_error.test.js:51:10 - - at __tests__/node_assertion_error.test.js:50:1 ● assert.notStrictEqual assert.notStrictEqual(received, expected) - + Expected value not be strictly equal to: 1 Received: 1 - + Message: My custom error message - + Difference: - + Compared values have no visual difference. 53 | @@ -723,25 +700,23 @@ exports[`works with node assert 1`] = ` 56 | }); 57 | 58 | test('assert.deepStrictEqual', () => { - + at __tests__/node_assertion_error.test.js:55:10 - - at __tests__/node_assertion_error.test.js:54:1 ● assert.deepStrictEqual assert.deepStrictEqual(received, expected) - + Expected value to deeply and strictly equal to: {\\"a\\": 2} Received: {\\"a\\": 1} - + Difference: - + - Expected + Received - + Object { - \\"a\\": 2, + \\"a\\": 1, @@ -754,22 +729,20 @@ exports[`works with node assert 1`] = ` 60 | }); 61 | 62 | test('assert.notDeepStrictEqual', () => { - + at __tests__/node_assertion_error.test.js:59:10 - - at __tests__/node_assertion_error.test.js:58:1 ● assert.notDeepStrictEqual assert.notDeepStrictEqual(received, expected) - + Expected value not to deeply and strictly equal to: {\\"a\\": 1} Received: {\\"a\\": 1} - + Difference: - + Compared values have no visual difference. 61 | @@ -779,21 +752,19 @@ exports[`works with node assert 1`] = ` 64 | }); 65 | 66 | test('assert.ifError', () => { - + at __tests__/node_assertion_error.test.js:63:10 - - at __tests__/node_assertion_error.test.js:62:1 ● assert.ifError ● assert.doesNotThrow assert.doesNotThrow(function) - + Expected the function not to throw an error. Instead, it threw: [Error: err!] - + Message: Got unwanted exception. @@ -804,18 +775,16 @@ exports[`works with node assert 1`] = ` 72 | throw Error('err!'); 73 | }); 74 | }); - + at __tests__/node_assertion_error.test.js:71:10 - - at __tests__/node_assertion_error.test.js:70:1 ● assert.throws assert.throws(function) - + Expected the function to throw an error. But it didn't throw anything. - + Message: Missing expected exception. @@ -825,10 +794,8 @@ exports[`works with node assert 1`] = ` | ^ 78 | }); 79 | - + at __tests__/node_assertion_error.test.js:77:10 - - at __tests__/node_assertion_error.test.js:76:1 " `; @@ -840,12 +807,12 @@ exports[`works with snapshot failures 1`] = ` ● failing snapshot expect(value).toMatchSnapshot() - + Received value does not match stored snapshot \\"failing snapshot 1\\". - + - Snapshot + Received - + - \\"bar\\" + \\"foo\\" @@ -855,7 +822,7 @@ exports[`works with snapshot failures 1`] = ` | ^ 13 | }); 14 | - + at __tests__/snapshot.test.js:12:17 › 1 snapshot failed. diff --git a/integration-tests/__tests__/__snapshots__/module_name_mapper.test.js.snap b/integration-tests/__tests__/__snapshots__/module_name_mapper.test.js.snap index 0d87e8bcdf33..f76ae0cc6f8b 100644 --- a/integration-tests/__tests__/__snapshots__/module_name_mapper.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/module_name_mapper.test.js.snap @@ -12,11 +12,11 @@ exports[`moduleNameMapper wrong configuration 1`] = ` ● Test suite failed to run Configuration error: - + Could not locate module ./style.css (mapped as no-such-module) - + Please check: - + \\"moduleNameMapper\\": { \\"/\\\\.(css|less)$/\\": \\"no-such-module\\" }, diff --git a/integration-tests/__tests__/failures.test.js b/integration-tests/__tests__/failures.test.js index 4b6a7e9bad07..4a129fe021de 100644 --- a/integration-tests/__tests__/failures.test.js +++ b/integration-tests/__tests__/failures.test.js @@ -42,11 +42,11 @@ test('works with node assert', () => { if (nodeMajorVersion >= 9) { expect(summary).toContain(` assert.doesNotThrow(function) - + Expected the function not to throw an error. Instead, it threw: [Error: err!] - + Message: Got unwanted exception. `); @@ -59,7 +59,7 @@ test('works with node assert', () => { 72 | throw Error('err!'); 73 | }); 74 | }); - + at __tests__/node_assertion_error.test.js:71:10 `); @@ -89,17 +89,17 @@ test('works with node assert', () => { if (nodeMajorVersion >= 10) { const ifErrorMessage = ` assert.ifError(received, expected) - + Expected value ifError to: null Received: 1 - + Message: ifError got unwanted exception: 1 - + Difference: - + Comparing two different types of values. Expected null but received number. 65 | @@ -109,10 +109,8 @@ test('works with node assert', () => { 68 | }); 69 | 70 | test('assert.doesNotThrow', () => { - + at __tests__/node_assertion_error.test.js:67:10 - - at __tests__/node_assertion_error.test.js:66:1 `; expect(summary).toContain(ifErrorMessage); @@ -128,8 +126,7 @@ test('works with node assert', () => { 67 | assert.ifError(1); 68 | }); 69 | - - + at packages/jest-jasmine2/build/jasmine/Spec.js:85:20 at __tests__/node_assertion_error.test.js:66:1 `; diff --git a/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap index 0e03ed6d61b8..af577d3e0811 100644 --- a/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/to_throw_matchers.test.js.snap @@ -14,7 +14,7 @@ exports[`.toThrow() error class threw, but class did not match 1`] = ` Expected the function to throw an error of type: \\"Err2\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -24,7 +24,7 @@ exports[`.toThrow() error class threw, but should not have 1`] = ` Expected the function not to throw an error of type: \\"Err\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -56,7 +56,7 @@ exports[`.toThrow() promise/async throws if Error-like object is returned threw, Expected the function to throw an error of type: "Err2" Instead, it threw: - Error  + Error  at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)] `; @@ -65,7 +65,7 @@ exports[`.toThrow() promise/async throws if Error-like object is returned threw, Expected the function not to throw an error. Instead, it threw: - Error  + Error  at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)] `; @@ -83,7 +83,7 @@ exports[`.toThrow() regexp threw, but message did not match 1`] = ` Expected the function to throw an error matching: /banana/ Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -93,7 +93,7 @@ exports[`.toThrow() regexp threw, but should not have 1`] = ` Expected the function not to throw an error matching: /apple/ Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -111,7 +111,7 @@ exports[`.toThrow() strings threw, but message did not match 1`] = ` Expected the function to throw an error matching: \\"banana\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -121,7 +121,7 @@ exports[`.toThrow() strings threw, but should not have 1`] = ` Expected the function not to throw an error matching: \\"apple\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -139,7 +139,7 @@ exports[`.toThrowError() error class threw, but class did not match 1`] = ` Expected the function to throw an error of type: \\"Err2\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -149,7 +149,7 @@ exports[`.toThrowError() error class threw, but should not have 1`] = ` Expected the function not to throw an error of type: \\"Err\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -181,7 +181,7 @@ exports[`.toThrowError() promise/async throws if Error-like object is returned t Expected the function to throw an error of type: "Err2" Instead, it threw: - Error  + Error  at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)] `; @@ -190,7 +190,7 @@ exports[`.toThrowError() promise/async throws if Error-like object is returned t Expected the function not to throw an error. Instead, it threw: - Error  + Error  at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)] `; @@ -208,7 +208,7 @@ exports[`.toThrowError() regexp threw, but message did not match 1`] = ` Expected the function to throw an error matching: /banana/ Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -218,7 +218,7 @@ exports[`.toThrowError() regexp threw, but should not have 1`] = ` Expected the function not to throw an error matching: /apple/ Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -236,7 +236,7 @@ exports[`.toThrowError() strings threw, but message did not match 1`] = ` Expected the function to throw an error matching: \\"banana\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -246,6 +246,6 @@ exports[`.toThrowError() strings threw, but should not have 1`] = ` Expected the function not to throw an error matching: \\"apple\\" Instead, it threw: - Error + Error at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; diff --git a/packages/jest-jasmine2/src/expectation_result_factory.js b/packages/jest-jasmine2/src/expectation_result_factory.js index 32e04989a969..d502ce6e0cad 100644 --- a/packages/jest-jasmine2/src/expectation_result_factory.js +++ b/packages/jest-jasmine2/src/expectation_result_factory.js @@ -36,12 +36,18 @@ function stackFormatter(options, initError, errorMessage) { return ''; } - if (options.error && options.error.stack) { - return options.error.stack; + if (options.error) { + if (options.error.stack) { + return options.error.stack; + } + + if (options.error === errorMessage) { + return errorMessage; + } } if (initError) { - return errorMessage + '\n' + initError.stack; + return errorMessage.trimRight() + '\n\n' + initError.stack; } return new Error(errorMessage).stack; diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap index 07a328bd79b2..a39921bd1894 100644 --- a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap +++ b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap @@ -17,10 +17,8 @@ exports[`formatStackTrace should strip node internals 1`] = ` \\"\\" type: \\"string\\" - - + at Object.it (__tests__/test.js:8:14) - " `; @@ -28,7 +26,7 @@ exports[`should exclude jasmine from stack trace for Unix paths. 1`] = ` " Unix test at stack (../jest-jasmine2/build/jasmine-2.4.1.js:1580:17) - + at Object.addResult (../jest-jasmine2/build/jasmine-2.4.1.js:1550:14) at Object.it (build/__tests__/messages-test.js:45:41) " @@ -44,10 +42,8 @@ exports[`should not exclude vendor from stack trace 1`] = ` \\"\\" type: \\"string\\" - - + at Object.it (__tests__/vendor/cool_test.js:6:666) at Object.asyncFn (__tests__/vendor/sulu/node_modules/sulu-content-bundle/best_component.js:1:5) - " `; diff --git a/packages/jest-message-util/src/index.js b/packages/jest-message-util/src/index.js index 941d03c5c166..6013d4ee71c8 100644 --- a/packages/jest-message-util/src/index.js +++ b/packages/jest-message-util/src/index.js @@ -64,6 +64,12 @@ const STACK_PATH_REGEXP = /\s*at.*\(?(\:\d*\:\d*|native)\)?/; const EXEC_ERROR_MESSAGE = 'Test suite failed to run'; const ERROR_TEXT = 'Error: '; +const indentAllLines = (lines: string, indent: string) => + lines + .split('\n') + .map(line => (line ? indent + line : line)) + .join('\n'); + const trim = string => (string || '').trim(); // Some errors contain not only line numbers in stack traces @@ -84,10 +90,7 @@ const getRenderedCallsite = ( {highlightCode: true}, ); - renderedCallsite = renderedCallsite - .split('\n') - .map(line => MESSAGE_INDENT + line) - .join('\n'); + renderedCallsite = indentAllLines(renderedCallsite, MESSAGE_INDENT); renderedCallsite = `\n${renderedCallsite}\n`; return renderedCallsite; @@ -127,10 +130,8 @@ export const formatExecError = ( message = separated.message; } - message = message - .split(/\n/) - .map(line => MESSAGE_INDENT + line) - .join('\n'); + message = indentAllLines(message, MESSAGE_INDENT); + stack = stack && !options.noStackTrace ? '\n' + formatStackTrace(stack, config, options, testPath) @@ -277,6 +278,7 @@ export const formatStackTrace = ( } const stacktrace = lines + .filter(Boolean) .map( line => STACK_INDENT + @@ -284,7 +286,7 @@ export const formatStackTrace = ( ) .join('\n'); - return renderedCallsite + stacktrace; + return `${renderedCallsite}\n${stacktrace}`; }; export const formatResultsErrors = ( @@ -311,10 +313,7 @@ export const formatResultsErrors = ( formatStackTrace(stack, config, options, testPath), ) + '\n'; - message = message - .split(/\n/) - .map(line => MESSAGE_INDENT + line) - .join('\n'); + message = indentAllLines(message, MESSAGE_INDENT); const title = chalk.bold.red(