JEST失败的覆盖范围报告没有'没有元素索引'

发布于 2025-02-04 01:24:30 字数 1145 浏览 5 评论 0原文

我正在使用使用Redux Toolkit的Next.js应用程序运行Jest Test Suites。我正在使用MSW嘲笑由Redux Toolkit处理的下一个应用程序之外其他API的任何必要的呼叫。

我还没有嘲笑所有RTK查询端点,只有我当前在测试中使用的端点。


当我通过NPM运行测试运行时运行时,测试正常运行。

但是,当我通过npm运行测试进行覆盖范围报告-Coverage时,所有测试都会失败,它们都有相同的错误:

- Test suite failed to run

    No element indexed by 9
    
     at ArraySet_at [as at] (node_modules/source-map-support/node_modules/source-map/lib/array-set.js:109:9)
      at BasicSourceMapConsumer.SourceMapConsumer_originalPositionFor [as originalPositionFor] (node_modules/source-map-support/node_modules/source-map/lib/source-map-consumer.js:673:30)
      at mapSourcePosition (node_modules/source-map-support/source-map-support.js:214:42)
      at wrapCallSite (node_modules/source-map-support/source-map-support.js:358:20)
      at node_modules/source-map-support/source-map-support.js:399:26
          at Array.map (<anonymous>)
      at Function.prepareStackTrace (node_modules/source-map-support/source-map-support.js:398:30)

我发现,如果我将MSW处理程序设置为空数组,错误未出现。如预期的那样,需要API失败的少数测试。但是,测试否则运行良好。

我不确定从这里去哪里。任何建议都非常感谢。

I am running jest test suites for a Next.js app that uses Redux Toolkit. I'm using msw to mock any necessary calls to another api outside of the Next app that are handled by Redux Toolkit.

I have not mocked ALL of the RTK query endpoints, only the ones which I am currently using in my tests.


The tests run fine when I'm running without coverage generation via npm run test.

However, when I run with coverage reports via npm run test -- --coverage, all tests fail and they all have the same error:

- Test suite failed to run

    No element indexed by 9
    
     at ArraySet_at [as at] (node_modules/source-map-support/node_modules/source-map/lib/array-set.js:109:9)
      at BasicSourceMapConsumer.SourceMapConsumer_originalPositionFor [as originalPositionFor] (node_modules/source-map-support/node_modules/source-map/lib/source-map-consumer.js:673:30)
      at mapSourcePosition (node_modules/source-map-support/source-map-support.js:214:42)
      at wrapCallSite (node_modules/source-map-support/source-map-support.js:358:20)
      at node_modules/source-map-support/source-map-support.js:399:26
          at Array.map (<anonymous>)
      at Function.prepareStackTrace (node_modules/source-map-support/source-map-support.js:398:30)

I found that if I set the msw handlers to an empty array, the error doesn't appear. The few tests which require the API fail, as expected. However, the tests all run fine otherwise.

I'm unsure where to go from here. Any advice is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2025-02-11 01:24:30

现在应该解决此

错误开玩笑到最新版本。


似乎这个问题与将Babel用作代码覆盖提供商的玩笑有关。这就是为什么切换到v8 上工作

如果您不想使用V8(当前标记为实验)。然后,您需要将Babel安装到您的构建中:

npm install --save-dev babel-jest @babel/core @babel/preset-env

source

上面的错误消失了。

This bug should now be fixed

If your still having this problem, upgrade Jest to the latest version.


It seems this issue is to do with jests use of babel as the code coverage provider. Which is why switching to v8 works around this issue.

If you don't want to use v8 (this is marked as experimental currently). Then you will need to install babel into your build:

npm install --save-dev babel-jest @babel/core @babel/preset-env

Source

which should make the above error go away.

相权↑美人 2025-02-11 01:24:30

我遇到了几乎相同的问题,但是我的情况错误消息是没有由55索引的元素。
我正在使用开玩笑,如果在一个测试中,我希望一个对象没有某些属性,我会遇到此错误。

然后我关闭了代码覆盖范围,问题消失了,测试通过了。但是我不想为此关闭它。

最后,对我来说,唯一的解决方案变成了 CoverageProvider 在Jest Config到 v8 (默认情况下,它使用babel)。

I had almost the same issue, but I my case error message was No element indexed by 55.
I am using Jest, and in case when in one test I expect an object not to have certain property, I've got this error.

Then I turned off code coverage and the problem disappeared and tests were passed. But I didn't want to turn it off for that.

Finally the only solution for me became changing coverageProvider in jest config to v8 (by default it uses babel).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文