Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest not showing errors that are thrown

Tags:

node.js

jestjs

I'm using jest for my unit tests and I have an issue when my code throws an unexpected exception, jest is not handling it.

For example:

async function func() {
    throw new Error('ERROR');
}

test('test', async () => {
    await func();
});

I would expect from jest to show me where is the exception but all I get is:

TypeError: jasmine.Spec.isPendingSpecException is not a function

  at returnValue.then.error (node_modules/jest-config/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:112:28)

Should I wrap the test function with try/catch and use fail() on the catch block?

I'm using the latest 24.0.0 version.

like image 296
Amir Rossert Avatar asked Oct 27 '25 14:10

Amir Rossert


1 Answers

I just ran into this myself. In my case, it is caused by a custom reporter that Intellij IDEA uses that is not compatible with version 24.0.0. If you downgrade to 23.6.0 it will work as you expect. To get the actual test error in version 24.0.0 just wrap the await statement in a try/catch like so:

test('test', async () => {
     try {
         await func();
     } catch (e) {
         fail(e)
     }
})

I have submitted an issue with Intellij. If you want to track the issue: jest-intellij-reporter is failing with TypeError

like image 84
phil b Avatar answered Oct 30 '25 03:10

phil b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!