Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom async matcher stack traces #7652

Merged
merged 10 commits into from Feb 13, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -116,6 +116,7 @@
- `[jest-config]` Normalize `config.cwd` and `config.rootDir` using `realpath ([#7598](https://github.com/facebook/jest/pull/7598))
- `[jest-environment-node]` Fix buffer property is not ArrayBuffer issue. ([#7626](https://github.com/facebook/jest/pull/7626))
- `[babel-plugin-jest-hoist]` Ignore TS type annotations when looking for out-of-scope references ([#7641](https://github.com/facebook/jest/pull/7641))
- `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652))

### Chore & Maintenance

Expand Down
61 changes: 50 additions & 11 deletions e2e/__tests__/__snapshots__/customMatcherStackTrace.test.js.snap
@@ -1,12 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`custom async matchers 1`] = `
Object {
"rest": "FAIL __tests__/async.test.js
✕ showing the stack trace for an async matcher

● showing the stack trace for an async matcher

We expect the stack trace and code fence for this matcher to be shown in the console.

2 |
3 | test('showing the stack trace for an async matcher', async () => {
> 4 | await expect(true).toThrowCustomAsyncMatcherError();
| ^
5 | });
6 |
7 | async function toThrowCustomAsyncMatcherError() {

at Object.toThrowCustomAsyncMatcherError (__tests__/async.test.js:4:22)

",
"summary": "Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /async.test.js/i.
",
}
`;

exports[`works with custom matchers 1`] = `
"FAIL __tests__/customMatcher.test.js
Custom matcher
✓ passes
✓ fails
✕ preserves error stack
"FAIL __tests__/async.test.js
● showing the stack trace for an async matcher

We expect the stack trace and code fence for this matcher to be shown in the console.

2 |
3 | test('showing the stack trace for an async matcher', async () => {
> 4 | await expect(true).toThrowCustomAsyncMatcherError();
| ^
5 | });
6 |
7 | async function toThrowCustomAsyncMatcherError() {

at Object.toThrowCustomAsyncMatcherError (__tests__/async.test.js:4:22)

FAIL __tests__/sync.test.js
● Custom matcher › preserves error stack

qux
Expand All @@ -19,12 +58,12 @@ exports[`works with custom matchers 1`] = `
47 |
48 | // This expecation fails due to an error we throw (intentionally)

at Error (__tests__/customMatcher.test.js:45:13)
at baz (__tests__/customMatcher.test.js:43:23)
at bar (__tests__/customMatcher.test.js:42:23)
at foo (__tests__/customMatcher.test.js:52:7)
at Object.callback (__tests__/customMatcher.test.js:11:18)
at Object.toCustomMatch (__tests__/customMatcher.test.js:53:8)
at Error (__tests__/sync.test.js:45:13)
at baz (__tests__/sync.test.js:43:23)
at bar (__tests__/sync.test.js:42:23)
at foo (__tests__/sync.test.js:52:7)
at Object.callback (__tests__/sync.test.js:11:18)
at Object.toCustomMatch (__tests__/sync.test.js:53:8)

"
`;
10 changes: 9 additions & 1 deletion e2e/__tests__/customMatcherStackTrace.test.js
Expand Up @@ -12,7 +12,7 @@ import runJest from '../runJest';
import {extractSummary} from '../Utils';

test('works with custom matchers', () => {
const {stderr} = runJest('custom-matcher-stack-trace');
const {stderr} = runJest('custom-matcher-stack-trace', ['sync.test.js']);

let {rest} = extractSummary(stderr);

Expand All @@ -23,3 +23,11 @@ test('works with custom matchers', () => {

expect(rest).toMatchSnapshot();
});

test('custom async matchers', () => {
const {stderr} = runJest('custom-matcher-stack-trace', ['async.test.js']);

const result = extractSummary(stderr);

expect(result).toMatchSnapshot();
theotherdon marked this conversation as resolved.
Show resolved Hide resolved
theotherdon marked this conversation as resolved.
Show resolved Hide resolved
});
11 changes: 11 additions & 0 deletions e2e/custom-matcher-stack-trace/__tests__/async.test.js
@@ -0,0 +1,11 @@
expect.extend({toThrowCustomAsyncMatcherError});

test('showing the stack trace for an async matcher', async () => {
await expect(true).toThrowCustomAsyncMatcherError();
});

async function toThrowCustomAsyncMatcherError() {
const message = () =>
'We expect the stack trace and code fence for this matcher to be shown in the console.';
return {message, pass: false};
}
14 changes: 12 additions & 2 deletions packages/expect/src/index.js
Expand Up @@ -251,7 +251,10 @@ const makeThrowingMatcher = (
utils,
};

const processResult = (result: SyncExpectationResult) => {
const processResult = (
result: SyncExpectationResult,
asyncError?: JestAssertionError,
) => {
_validateResult(result);

getState().assertionCalls++;
Expand All @@ -264,6 +267,9 @@ const makeThrowingMatcher = (
if (err) {
error = err;
error.message = message;
} else if (asyncError) {
error = asyncError;
error.message = message;
} else {
error = new JestAssertionError(message);

Expand Down Expand Up @@ -307,9 +313,13 @@ const makeThrowingMatcher = (

if (isPromise((potentialResult: any))) {
const asyncResult = ((potentialResult: any): AsyncExpectationResult);
const asyncError = new JestAssertionError();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, throwingMatcher);
}

return asyncResult
.then(aResult => processResult(aResult))
.then(aResult => processResult(aResult, asyncError))
Copy link

@erikpuk erikpuk Oct 21, 2019

Choose a reason for hiding this comment

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

I'm having some issues with this code, 8 months on. I'm almost certainly not understanding correctly, but it seems to me....

  1. asyncError is always truthy here. Even though this is the normal codepath that any async matcher will follow

  2. In processResult we just test the truthiness of asyncError to decide whether an error was thrown.

  3. That means we end up throwing a normal { message, pass } assertion result, which is not what is supposed to happen.

But.... what that suggests to me is that async custom would just be fully broken all the time, which.... I mean, I assume they're tested? So I assume I'm missing something?

But for me, they are in fact broken all the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What issue are you having? The same one that I fixed in the description or a different one? It's been a while since I worked on this, so I don't remember what we're supposed to be throwing for point 3. Can you remind me what we're supposed to be throwing instead?

asyncError will indeed always be defined if we're using async matchers. However, the only time we do something with the asyncError is when the assertion fails because of this conditional here.

A little bit of context that I'm not sure you're aware of, but I think might be helpful. We always need to pass in an error to preserve the stack trace when we're working with async code. If we don't, then the stack trace is lost when we hit the next tick in the event loop. That's why the async error is always being initialized and passed in. It's just not used unless we hit an error state. Unless I'm misunderstanding what you're saying?

.catch(error => handlError(error));
} else {
const syncResult = ((potentialResult: any): SyncExpectationResult);
Expand Down