Expo & Jest: Jest encountered an unexpected token

I’m trying to test an Expo managed project using expo-jest and I keep getting this error no matter what I try. None of the other answers from SO have worked so far.

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it’s not plain JavaScript.

Can anyone help?

Here is my package.json:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest"
  },
  "dependencies": 
   ...
  },
  "devDependencies": {
    "@babel/cli": "^7.14.3",
    "@babel/core": "^7.9.6",
    "@babel/preset-env": "^7.14.4",
    "@babel/preset-react": "^7.13.13",
    "babel-plugin-transform-es2015-destructuring": "^6.23.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "enzyme-adapter-react-15": "^1.4.3",
    "eslint-config-airbnb": "^18.2.1",
    "jest-expo": "^41.0.0",
    "react-test-renderer": "^17.0.2"
  },
  "jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)"
    ]
  },
  "private": true,
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
}

And here is my babel.config.js

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin']
  };
};

And my App.test.js file:

import React from 'react';
import renderer from 'react-test-renderer';

import App from '../App';

describe('<App />', () => {
  it('has 1 child', () => {
    const tree = renderer.create(<App />).toJSON();
    expect(tree.children.length).toBe(1);
  });
});

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.