1

I am attempting to get Jest working for my React Native project and have run into a variety of problems, the most confusing of which is the following:

When I run any test that renders a component, it spits out the error No element indexed by 7.

Here is the full stack trace:

FAIL  __tests__/App-test.tsx
      ● Test suite failed to run
    
        No element indexed by 7
    
          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:244:42)
          at wrapCallSite (node_modules/source-map-support/source-map-support.js:397:20)
          at Function.prepareStackTrace (node_modules/source-map-support/source-map-support.js:446:39)
          at Function.write (node_modules/@jest/console/build/BufferedConsole.js:101:7)
          at console._log (node_modules/@jest/console/build/BufferedConsole.js:117:21)
          at console.error (node_modules/@jest/console/build/BufferedConsole.js:161:10)

This same error occurs with any component I attempt to render.

Regarding this issue which purports to solve a similar problem, I have tried installing babel (npm install --save-dev babel-jest @babel/core @babel/preset-env) and setting "coverageProvider" in the jest config to "v8". With or without the v8 preset I am still getting the same errors. It's definitely possible that I configured something else wrong. Here are some code snippets which may be of use:

App-test.tsx

/**
 * @format
 */

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});

package.json

// ...
"jest": {
    "preset": "react-native",
    "coverageProvider": "babel",
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native|@react-native|react-native-video|react-native-reanimated|@miblanchard/react-native-slider|react-native-gesture-handler)/)"
    ],
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    },
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }

babel.config.js

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    ['@babel/preset-typescript', {allowDeclareFields: true}],
  ],
  plugins: [
    "react-native-reanimated/plugin",
  ],
};

As an additional note, I have tried adding @babel/preset-env to the list of babel presets, but this only resulted in a different error related to a separate package added to the transformIgnorePatterns list. Adding source-map-support to said list also solves nothing.

Edited to note that these tests fail regardless of whether or not the --coverage argument is applied

1 Answers1

0

In the end, I solved my error by uninstalling node_modules, re-running npm install --save-dev babel-jest @babel/core @babel/preset-env and adding

"setupFiles": [
      "./node_modules/react-native-gesture-handler/jestSetup.js",
      // ...
]

to my package.json