Jest fails coverage reports with 'No element indexed'
Asked Answered
S

2

8

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.

Stockroom answered 3/6, 2022 at 15:11 Comment(0)
G
4

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

Grumous answered 4/6, 2022 at 13:45 Comment(0)
S
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.

Sacrament answered 8/6, 2022 at 10:5 Comment(2)
I've raised a bug with jestSacrament
Thank you so much for explaining this further.Stockroom
G
4

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

Grumous answered 4/6, 2022 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.