Skip to content

Commit

Permalink
fix: show stack traces on async assert errors
Browse files Browse the repository at this point in the history
Fixes #6752
  • Loading branch information
SimenB committed Sep 29, 2018
1 parent e2dac2b commit a12f081
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- `[jest-cli]` Update jest-cli to show git ref in message when using `changedSince` ([#7028](https://github.com/facebook/jest/pull/7028))
- `[jest-jasmine2`] Fix crash when test return Promise rejected with null ([#7049](https://github.com/facebook/jest/pull/7049))
- `[jest-runtime]` Check `_isMockFunction` is true rather than truthy on potential global mocks ([#7017](https://github.com/facebook/jest/pull/7017))
- `[jest-jasmine]` Show proper error message from async `assert` errors ([#6821](https://github.com/facebook/jest/pull/6821))

### Chore & Maintenance

Expand Down
35 changes: 35 additions & 0 deletions e2e/__tests__/__snapshots__/failures.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ exports[`works with node assert 1`] = `
✕ assert.ifError
✕ assert.doesNotThrow
✕ assert.throws
✕ async
● assert
Expand Down Expand Up @@ -809,9 +810,43 @@ exports[`works with node assert 1`] = `
| ^
78 | });
79 |
80 | test('async', async () => {
at __tests__/node_assertion_error.test.js:77:10
async
assert.equal(received, expected) or assert(received)
Expected value to be equal to:
\\"hello\\"
Received:
\\"hello
goodbye\\"
Message:
hmmm
Difference:
- Expected
+ Received
hello
+ goodbye
79 |
80 | test('async', async () => {
> 81 | assert.equal('hello\\\\ngoodbye', 'hello', 'hmmm');
| ^
82 | });
83 |
at __tests__/node_assertion_error.test.js:81:10
at __tests__/node_assertion_error.test.js:12:191
at __tests__/node_assertion_error.test.js:12:437
at __tests__/node_assertion_error.test.js:12:99
"
`;
Expand Down
4 changes: 4 additions & 0 deletions e2e/failures/__tests__/node_assertion_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ test('assert.doesNotThrow', () => {
test('assert.throws', () => {
assert.throws(() => {});
});

test('async', async () => {
assert.equal('hello\ngoodbye', 'hello', 'hmmm');
});
15 changes: 14 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* eslint-disable sort-keys */

import {AssertionError} from 'assert';
import queueRunner from '../queue_runner';
import treeProcessor from '../tree_processor';
import checkIsError from '../is_error';
import assertionErrorMessage from '../assert_support';

// Try getting the real promise object from the context, if available. Someone
// could have overridden it in a test. Async functions return it implicitly.
Expand Down Expand Up @@ -547,7 +549,18 @@ export default function(j$) {
};

this.fail = function(error) {
const {isError, message} = checkIsError(error);
let isError;
let message;

if (error instanceof AssertionError) {
isError = false;
message = assertionErrorMessage(error, {expand: j$.Spec.expand});
} else {
const check = checkIsError(error);

isError = check.isError;
message = check.message;
}

currentRunnable().addExpectationResult(false, {
matcherName: '',
Expand Down

0 comments on commit a12f081

Please sign in to comment.