Skip to content

Commit

Permalink
jest-jasmine2: pretty-print non-Error errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Apr 13, 2018
1 parent 3919925 commit 64498e7
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 49 deletions.
124 changes: 87 additions & 37 deletions integration-tests/__tests__/__snapshots__/failures.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,55 @@ exports[`not throwing Error objects 4`] = `
"
`;
exports[`not throwing Error objects 5`] = `
"FAIL __tests__/during_tests.test.js
✕ Promise thrown during test
✕ Boolean thrown during test
✕ undefined thrown during test
✕ Object thrown during test
✕ Error during test
● Promise thrown during test
thrown: Promise {}
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
● Boolean thrown during test
thrown: false
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
● undefined thrown during test
thrown: undefined
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
● Object thrown during test
thrown: Object {
\\"notAnError\\": true,
}
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
● Error during test
ReferenceError: doesNotExist is not defined
18 |
19 | test('Error during test', () => {
> 20 | doesNotExist.alsoThisNot;
21 | });
22 |
at __tests__/during_tests.test.js:20:3
"
`;
exports[`works with assertions in separate files 1`] = `
"FAIL __tests__/test_macro.test.js
✕ use some imported macro to make assertion
Expand Down Expand Up @@ -142,7 +191,7 @@ exports[`works with node assert 1`] = `
● assert
assert.equal(received, expected) or assert(received)
thrown: \\"assert.equal(received, expected) or assert(received)
Expected value to be (operator: ==):
true
Expand All @@ -160,7 +209,7 @@ exports[`works with node assert 1`] = `
assert with a message
assert.equal(received, expected) or assert(received)
thrown: \\"assert.equal(received, expected) or assert(received)
Expected value to be (operator: ==):
true
Expand All @@ -181,7 +230,7 @@ exports[`works with node assert 1`] = `
assert.ok
assert.equal(received, expected) or assert(received)
thrown: \\"assert.equal(received, expected) or assert(received)
Expected value to be (operator: ==):
true
Expand All @@ -199,7 +248,7 @@ exports[`works with node assert 1`] = `
assert.ok with a message
assert.equal(received, expected) or assert(received)
thrown: \\"assert.equal(received, expected) or assert(received)
Expected value to be (operator: ==):
true
Expand All @@ -220,7 +269,7 @@ exports[`works with node assert 1`] = `
assert.equal
assert.equal(received, expected) or assert(received)
thrown: \\"assert.equal(received, expected) or assert(received)
Expected value to be (operator: ==):
2
Expand All @@ -238,7 +287,7 @@ exports[`works with node assert 1`] = `
assert.notEqual
assert.notEqual(received, expected)
thrown: \\"assert.notEqual(received, expected)
Expected value not to be (operator: !=):
1
Expand All @@ -260,23 +309,23 @@ exports[`works with node assert 1`] = `
assert.deepEqual
assert.deepEqual(received, expected)
thrown: \\"assert.deepEqual(received, expected)
Expected value to deeply equal to:
{\\"a\\": {\\"b\\": {\\"c\\": 6}}}
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 6}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 5}}}
Difference:
- Expected
+ Received
Object {
\\"a\\": Object {
\\"b\\": Object {
- \\"c\\": 6,
+ \\"c\\": 5,
\\\\\\"a\\\\\\": Object {
\\\\\\"b\\\\\\": Object {
- \\\\\\"c\\\\\\": 6,
+ \\\\\\"c\\\\\\": 5,
},
},
}
Expand All @@ -292,12 +341,12 @@ exports[`works with node assert 1`] = `
assert.deepEqual with a message
assert.deepEqual(received, expected)
thrown: \\"assert.deepEqual(received, expected)
Expected value to deeply equal to:
{\\"a\\": {\\"b\\": {\\"c\\": 7}}}
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 7}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 5}}}
Message:
this is a message
Expand All @@ -308,10 +357,10 @@ exports[`works with node assert 1`] = `
+ Received
Object {
\\"a\\": Object {
\\"b\\": Object {
- \\"c\\": 7,
+ \\"c\\": 5,
\\\\\\"a\\\\\\": Object {
\\\\\\"b\\\\\\": Object {
- \\\\\\"c\\\\\\": 7,
+ \\\\\\"c\\\\\\": 5,
},
},
}
Expand All @@ -327,12 +376,12 @@ exports[`works with node assert 1`] = `
assert.notDeepEqual
assert.notDeepEqual(received, expected)
thrown: \\"assert.notDeepEqual(received, expected)
Expected value not to deeply equal to:
{\\"a\\": 1}
{\\\\\\"a\\\\\\": 1}
Received:
{\\"a\\": 1}
{\\\\\\"a\\\\\\": 1}
Difference:
Expand All @@ -349,7 +398,7 @@ exports[`works with node assert 1`] = `
assert.strictEqual
assert.strictEqual(received, expected)
thrown: \\"assert.strictEqual(received, expected)
Expected value to be (operator: ===):
NaN
Expand All @@ -367,7 +416,7 @@ exports[`works with node assert 1`] = `
assert.notStrictEqual
assert.notStrictEqual(received, expected)
thrown: \\"assert.notStrictEqual(received, expected)
Expected value not to be (operator: !==):
1
Expand All @@ -392,21 +441,21 @@ exports[`works with node assert 1`] = `
assert.deepStrictEqual
assert.deepStrictEqual(received, expected)
thrown: \\"assert.deepStrictEqual(received, expected)
Expected value to deeply and strictly equal to:
{\\"a\\": 2}
{\\\\\\"a\\\\\\": 2}
Received:
{\\"a\\": 1}
{\\\\\\"a\\\\\\": 1}
Difference:
- Expected
+ Received
Object {
- \\"a\\": 2,
+ \\"a\\": 1,
- \\\\\\"a\\\\\\": 2,
+ \\\\\\"a\\\\\\": 1,
}
57 |
Expand All @@ -420,12 +469,12 @@ exports[`works with node assert 1`] = `
assert.notDeepStrictEqual
assert.notDeepStrictEqual(received, expected)
thrown: \\"assert.notDeepStrictEqual(received, expected)
Expected value not to deeply and strictly equal to:
{\\"a\\": 1}
{\\\\\\"a\\\\\\": 1}
Received:
{\\"a\\": 1}
{\\\\\\"a\\\\\\": 1}
Difference:
Expand All @@ -442,12 +491,13 @@ exports[`works with node assert 1`] = `
assert.ifError
Error
1 thrown
thrown: 1
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
assert.doesNotThrow
assert.doesNotThrow(function)
thrown: \\"assert.doesNotThrow(function)
Expected the function not to throw an error.
Instead, it threw:
Expand All @@ -467,7 +517,7 @@ exports[`works with node assert 1`] = `
assert.throws
assert.throws(function)
thrown: \\"assert.throws(function)
Expected the function to throw an error.
But it didn't throw anything.
Expand Down
10 changes: 6 additions & 4 deletions integration-tests/__tests__/failures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ test('not throwing Error objects', () => {
expect(extractSummary(stderr).rest).toMatchSnapshot();
stderr = runJest(dir, ['assertion_count.test.js']).stderr;
expect(extractSummary(cleanupStackTrace(stderr)).rest).toMatchSnapshot();
stderr = runJest(dir, ['during_tests.test.js']).stderr;
expect(extractSummary(stderr).rest).toMatchSnapshot();
});

test('works with node assert', () => {
Expand All @@ -63,23 +65,23 @@ test('works with node assert', () => {
if (Number(process.versions.node.split('.')[0]) >= 9) {
expect(summary).toContain(`
assert.doesNotThrow(function)
Expected the function not to throw an error.
Instead, it threw:
[Error: err!]
Message:
Got unwanted exception.
err!
err!
69 |
69 |
70 | test('assert.doesNotThrow', () => {
> 71 | assert.doesNotThrow(() => {
72 | throw Error('err!');
73 | });
74 | });
at __tests__/node_assertion_error.test.js:71:10
`);

Expand Down
25 changes: 25 additions & 0 deletions integration-tests/failures/__tests__/during_tests.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

test('Promise thrown during test', () => {
throw Promise.resolve(5);
});

test('Boolean thrown during test', () => {
// eslint-disable-next-line no-throw-literal
throw false;
});

test('undefined thrown during test', () => {
// eslint-disable-next-line no-throw-literal
throw undefined;
});

test('Object thrown during test', () => {
// eslint-disable-next-line no-throw-literal
throw {notAnError: true};
});

test('Error during test', () => {
// eslint-disable-next-line no-undef
doesNotExist.alsoThisNot;
});
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"jest-message-util": "^22.4.0",
"jest-snapshot": "^22.4.0",
"jest-util": "^22.4.1",
"source-map-support": "^0.5.0"
"source-map-support": "^0.5.0",
"pretty-format": "^22.4.0"
},
"devDependencies": {
"jest-runtime": "^22.4.2"
Expand Down
12 changes: 5 additions & 7 deletions packages/jest-jasmine2/src/expectation_result_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@
* @flow
*/

import prettyFormat from 'pretty-format';

function messageFormatter({error, message, passed}) {
if (passed) {
return 'Passed.';
}
if (message) {
return message;
}
if (!error) {
return '';
}
return error.message && error.name
? `${error.name}: ${error.message}`
: `${error.toString()} thrown`;
return `thrown: ${prettyFormat(error, {maxDepth: 3})}`;
}

function stackFormatter(options, errorMessage) {
if (options.passed) {
return '';
}
const {stack} = options.error || new Error(errorMessage);
const stack =
(options.error && options.error.stack) || new Error(errorMessage).stack;
return stack;
}

Expand Down

0 comments on commit 64498e7

Please sign in to comment.