开玩笑没有显示抛出的错误 [英] Jest not showing errors that are thrown

查看:55
本文介绍了开玩笑没有显示抛出的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元测试中使用了笑话,但是当我的代码抛出意外的异常时,我却遇到了问题,笑话没有处理它.

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.

例如:

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)

我应该用try/catch包装测试函数,并在catch块上使用 fail()吗?

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

我正在使用最新的 24.0.0 版本.

I'm using the latest 24.0.0 version.

推荐答案

我自己遇到了这个问题.就我而言,这是由Intellij IDEA使用的自定义报告程序引起的,该报告程序与24.0.0版本不兼容.如果降级到23.6.0,它将按预期工作.要获得版本24.0.0中的实际测试错误,只需将await语句包装在try/catch中,如下所示:

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)
     }
})

我已经提交了与Intellij有关的问题.如果您想跟踪问题,请执行以下操作: jest-intellij-reporter出现TypeError失败

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

这篇关于开玩笑没有显示抛出的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆