最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest fails coverage reports with 'No element indexed' - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share edited Jun 8, 2022 at 10:02 Liam 29.8k28 gold badges138 silver badges202 bronze badges asked Jun 3, 2022 at 15:11 GurnzbotGurnzbot 4,1528 gold badges41 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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

发布评论

评论列表(0)

  1. 暂无评论