Jest: afterAll hook hides errors.

Created on 7 Apr 2017  ·  10Comments  ·  Source: facebook/jest


Do you want to request a feature or report a bug?

Bug 🐛

What is the current behavior?

Errors thrown in afterAll are caught somewhere in Jest, and can cause a test suite to fail silently.

See the repl here - https://repl.it/HAYG/1

What is the expected behavior?

The afterAll hook should respect errors that are thrown, the same way that beforeAll does.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

❯ node --version; npm --version; jest --version
v4.6.2
2.15.11
v19.0.0
Bug

Most helpful comment

Working on it :)

All 10 comments

In addition, this functionality is critical for global afterAll things like "fail the test if the test author failed to do some kind of critical cleanup task".

yes! we absolutely need this!
We inherited this behavior from jasmine, but now since we started the migration away from it we can actually fix it.

It could presumably be fixed upstream in jasmine as well?

Does jest not use jasmine internally? It looks like this works as intended in jasmine 2.5 at least.

So I dug into this a bit, and it looks like Jasmine is assigning most errors to Spec instances, but afterAll errors get assigned to Suite instances (same in Jasmine master).

It seems that Jest is only looking at exceptions that happen in Specs.

afterAll errors are handled here - https://github.com/facebook/jest/blob/master/packages/jest-jasmine2/src/jasmine/Suite.js#L156

Still looking for where Jest handles these.

So, you can get the results of the suite here - https://github.com/facebook/jest/blob/master/packages/jest-jasmine2/src/reporter.js#L73

There is an unused argument passed to suiteDone that is the result of the suite, which looks like this...

{ id: 'suite1',
      description: 'Jasmine2Reporter',
      fullName: 'Jasmine2Reporter',
      failedExpectations: 
       [ { actual: '',
           error: 
            Error: asdf
                at Object.afterAll (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/src/__tests__/reporter-test.js:22:11)
                at Object.asyncFn (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/jasmine-async.js:40:30)
                at e (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/queueRunner.js:39:12)
                at mapper (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/queueRunner.js:32:21)
                at Promise.resolve.then.el (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/node_modules/p-map/index.js:42:16)
                at process._tickCallback (internal/process/next_tick.js:109:7),
           expected: '',
           matcherName: '',
           message: 'Error: asdf',
           passed: false,
           stack: 'Error: asdf\n    at Object.afterAll (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/src/__tests__/reporter-test.js:22:11)\n    at Object.asyncFn (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/jasmine-async.js:40:30)\n    at e (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/queueRunner.js:39:12)\n    at mapper (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/build/queueRunner.js:32:21)\n    at Promise.resolve.then.el (/Users/gary_borton/airlab/repos/jest/packages/jest-jasmine2/node_modules/p-map/index.js:42:16)\n    at process._tickCallback (internal/process/next_tick.js:109:7)' } ],
      status: 'failed' }

This doesn't match against the SpecResult type, nor does it have all the required fields for a AssertionResult. This is starting to look like it'll require some cascading changes (new types, formatting the new types into messages, teaching Jest about suite failures or mapping it to a spec failure).

I don't think I'd be able to PR this any time soon.

any work around for this? :confused:

Different context on the same issue (marked as a duplicate): https://github.com/facebook/jest/issues/3785

Working on it :)

5884

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gustavjf picture gustavjf  ·  3Comments

StephanBijzitter picture StephanBijzitter  ·  3Comments

paularmstrong picture paularmstrong  ·  3Comments

jardakotesovec picture jardakotesovec  ·  3Comments

withinboredom picture withinboredom  ·  3Comments