Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add stacktraces to async matchers
  • Loading branch information
SimenB committed Apr 16, 2018
commit fbcb002c732afb1cbf6974ebe5b09291bcc393fa
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

* `[expect]` Add stack trace for async errors (TBD)
* `[jest-runtime]` Prevent modules from marking themselves as their own parent
([#5235](https://github.com/facebook/jest/issues/5235))
* `[jest-mock]` Add support for auto-mocking generator functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,49 @@ Object {

● fail with expected promise values

Error
Error: Expected value to have length:
2
Received:
1
received.length:
1
Expected value to have length:
2
Received:
1
received.length:
1

22 |
23 | it('fail with expected promise values', async () => {
> 24 | await (expect(Promise.resolve([1])): any).resolves.toHaveLengthAsync(
| ^
25 | Promise.resolve(2)
26 | );
27 | });

at packages/expect/build/index.js:165:22
at __tests__/failure.test.js:24:54
at __tests__/failure.test.js:11:191
at __tests__/failure.test.js:11:437
at __tests__/failure.test.js:11:99
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so long since the matcher is defined in that file - if the matcher came from e.g. node_modules it'd be shorter


● fail with expected promise values and not

Error
Error: Expected value to not have length:
2
Received:
1,2
received.length:
2
Expected value to not have length:
2
Received:
1,2
received.length:
2

28 |
29 | it('fail with expected promise values and not', async () => {
> 30 | await (expect(Promise.resolve([1, 2])).resolves.not: any).toHaveLengthAsync(
| ^
31 | Promise.resolve(2)
32 | );
33 | });

at packages/expect/build/index.js:165:22
at __tests__/failure.test.js:30:61
at __tests__/failure.test.js:11:191
at __tests__/failure.test.js:11:437
at __tests__/failure.test.js:11:99

",
"summary": "Test Suites: 1 failed, 1 total
Expand Down
83 changes: 80 additions & 3 deletions integration-tests/__tests__/__snapshots__/failures.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,12 @@ exports[`works with assertions in separate files 1`] = `

exports[`works with async failures 1`] = `
"FAIL __tests__/async_failures.test.js
✕ something async
✕ resolve, but fail
✕ reject, but fail
✕ expect reject
✕ expect resolve

something async
resolve, but fail

expect(received).toEqual(expected)

Expand All @@ -211,8 +214,82 @@ exports[`works with async failures 1`] = `
- \\"baz\\": \\"bar\\",
+ \\"foo\\": \\"bar\\",
}

10 |
11 | test('resolve, but fail', () => {
> 12 | return expect(Promise.resolve({foo: 'bar'})).resolves.toEqual({baz: 'bar'});
| ^
13 | });
14 |
15 | test('reject, but fail', () => {

at packages/expect/build/index.js:165:22
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\\",
}

14 |
15 | test('reject, but fail', () => {
> 16 | return expect(Promise.reject({foo: 'bar'})).rejects.toEqual({baz: 'bar'});
| ^
17 | });
18 |
19 | test('expect reject', () => {

at packages/expect/build/index.js:202:22
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\\"}

18 |
19 | test('expect reject', () => {
> 20 | return expect(Promise.resolve({foo: 'bar'})).rejects.toEqual({foo: 'bar'});
| ^
21 | });
22 |
23 | test('expect resolve', () => {

at packages/expect/build/index.js:96:15
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\\"}

22 |
23 | test('expect resolve', () => {
> 24 | return expect(Promise.reject({foo: 'bar'})).resolves.toEqual({foo: 'bar'});
| ^
25 | });
26 |

at packages/expect/build/index.js:160:61
at packages/expect/build/index.js:96:15
at __tests__/async_failures.test.js:24:10

"
`;
Expand Down
14 changes: 13 additions & 1 deletion integration-tests/failures/__tests__/async_failures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
*/
'use strict';

test('something async', () => {
test('resolve, but fail', () => {
return expect(Promise.resolve({foo: 'bar'})).resolves.toEqual({baz: 'bar'});
});

test('reject, but fail', () => {
return expect(Promise.reject({foo: 'bar'})).rejects.toEqual({baz: 'bar'});
});

test('expect reject', () => {
return expect(Promise.resolve({foo: 'bar'})).rejects.toEqual({foo: 'bar'});
});

test('expect resolve', () => {
return expect(Promise.reject({foo: 'bar'})).resolves.toEqual({foo: 'bar'});
});
65 changes: 43 additions & 22 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const expect = (actual: any, ...rest): ExpectationObject => {
resolves: {not: {}},
};

const err = new JestAssertionError();

Object.keys(allMatchers).forEach(name => {
const matcher = allMatchers[name];
const promiseMatcher = getPromiseMatcher(name, matcher) || matcher;
Expand All @@ -99,25 +101,29 @@ const expect = (actual: any, ...rest): ExpectationObject => {
promiseMatcher,
false,
actual,
err,
);
expectation.resolves.not[name] = makeResolveMatcher(
name,
promiseMatcher,
true,
actual,
err,
);

expectation.rejects[name] = makeRejectMatcher(
name,
promiseMatcher,
false,
actual,
err,
);
expectation.rejects.not[name] = makeRejectMatcher(
name,
promiseMatcher,
true,
actual,
err,
);
});

Expand All @@ -136,6 +142,7 @@ const makeResolveMatcher = (
matcher: RawMatcherFn,
isNot: boolean,
actual: Promise<any>,
outerErr: JestAssertionError,
): PromiseMatcherFn => (...args) => {
const matcherStatement = `.resolves.${isNot ? 'not.' : ''}${matcherName}`;
if (!isPromise(actual)) {
Expand All @@ -147,17 +154,19 @@ const makeResolveMatcher = (
);
}

const innerErr = new JestAssertionError();

return actual.then(
result => makeThrowingMatcher(matcher, isNot, result).apply(null, args),
result =>
makeThrowingMatcher(matcher, isNot, result, innerErr).apply(null, args),
reason => {
const err = new JestAssertionError(
outerErr.message =
utils.matcherHint(matcherStatement, 'received', '') +
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to resolve, ` +
'instead it rejected to value\n' +
` ${utils.printReceived(reason)}`,
);
return Promise.reject(err);
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to resolve, ` +
'instead it rejected to value\n' +
` ${utils.printReceived(reason)}`;
return Promise.reject(outerErr);
},
);
};
Expand All @@ -167,6 +176,7 @@ const makeRejectMatcher = (
matcher: RawMatcherFn,
isNot: boolean,
actual: Promise<any>,
outerErr: JestAssertionError,
): PromiseMatcherFn => (...args) => {
const matcherStatement = `.rejects.${isNot ? 'not.' : ''}${matcherName}`;
if (!isPromise(actual)) {
Expand All @@ -178,25 +188,28 @@ const makeRejectMatcher = (
);
}

const innerErr = new JestAssertionError();

return actual.then(
result => {
const err = new JestAssertionError(
outerErr.message =
utils.matcherHint(matcherStatement, 'received', '') +
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` +
'instead it resolved to value\n' +
` ${utils.printReceived(result)}`,
);
return Promise.reject(err);
'\n\n' +
`Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` +
'instead it resolved to value\n' +
` ${utils.printReceived(result)}`;
return Promise.reject(outerErr);
},
reason => makeThrowingMatcher(matcher, isNot, reason).apply(null, args),
reason =>
makeThrowingMatcher(matcher, isNot, reason, innerErr).apply(null, args),
);
};

const makeThrowingMatcher = (
matcher: RawMatcherFn,
isNot: boolean,
actual: any,
err?: JestAssertionError,
): ThrowingMatcherFn => {
return function throwingMatcher(...args): any {
let throws = true;
Expand All @@ -223,16 +236,24 @@ const makeThrowingMatcher = (
if ((result.pass && isNot) || (!result.pass && !isNot)) {
// XOR
const message = getMessage(result.message);
const error = new JestAssertionError(message);
let error;

if (err) {
error = err;
error.message = message;
} else {
error = new JestAssertionError(message);

// Try to remove this function from the stack trace frame.
// Guard for some environments (browsers) that do not support this feature.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, throwingMatcher);
}
}
// Passing the result of the matcher with the error so that a custom
// reporter could access the actual and expected objects of the result
// for example in order to display a custom visual diff
error.matcherResult = result;
// Try to remove this function from the stack trace frame.
// Guard for some environments (browsers) that do not support this feature.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, throwingMatcher);
}

if (throws) {
throw error;
Expand Down