Error when running jest on a react native + typescript app (Jest encountered an unexpected token)

maximedupre

It seems like everyone and their mother are having a variation of this problem. Nothing worked from everything I have tried from all the SO questions and GH tickets.

It should actually be quite simple, since my project is pretty much a new barebone project. Yet I still can't figure out what is wrong for the life of me.

When I run jest:

/Desktop/Dropbox/Programming/iphone-app/fe/App.spec.tsx:11
    const tree = react_test_renderer_1.default.create(<App_1.default />).toJSON();
                                                      ^

SyntaxError: Unexpected token <

My config files:

// jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

-

// tsconfig.json

{
    "compilerOptions": {
        "allowJs": false,
        "allowSyntheticDefaultImports": true,
        "noFallthroughCasesInSwitch": true,
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "isolatedModules": true,
        "jsx": "react-native",
        "lib": [
            "es6"
        ],
        "moduleResolution": "node",
        "noEmit": true,
        "strict": true,
        "target": "esnext"
    },
    "exclude": [
        "node_modules"
    ]
}

-

// babel.config.js

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

EDIT #1

Just to be clear, I have tried using the react-native preset in the jest config file, with no success (same error):

// jest.config.js

module.exports = {
    preset: 'react-native',
    transform: {
        '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
        '^.+\\.tsx?$': 'ts-jest'
    },
    globals: {
        'ts-jest': {
            tsConfig: 'tsconfig.json'
        }
    },
    testEnvironment: 'node',
};
maximedupre

It works with this config file:

const { defaults } = require('jest-config');

module.exports = {
    preset: 'react-native',
    transform: {
        '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
        '^.+\\.tsx?$': 'ts-jest'
    },
    moduleFileExtensions: [
        'tsx',
        ...defaults.moduleFileExtensions
    ],
};

I needed to add

...
moduleFileExtensions: [
    'tsx',
    ...defaults.moduleFileExtensions
],
...

Or else the import App from './App'; in my test would resolve to some other file. By adding tsx as the topmost priority in the moduleFileExtensions option, the app resolves the correct file.

Also the jsx compiler option in tsconfig.json needs to be set to "jsx": "react". I'm not sure why I can't set it to react-native yet, but everything seems to work for now.

EDIT

'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' is not needed in newer versions of react-native. Keeping it may also cause a problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jest encountered an unexpected token when working with React TypeScript

Jest encountered an unexpected token with react-native

React app: Jest encountered an unexpected token

testing React Native Application with Jest - Jest encountered an unexpected token

Jest encountered an unexpected token (React, Typescript, Babel, Jest & Webpack setup)

Jest encountered an unexpected token in typescript

Jest encountered an unexpected token React

Suppress: Jest encountered an unexpected token from React Typescript

Jest encountered an unexpected token when importing lodash functions in Typescript

React native typescript throws error when running jest test command

Jest encountered an unexpected token

Jest encountered an unexpected token ReactJS

Jest encountered an unexpected token nestjs

Unexpected import token - testing React Native with Jest

Jest Error Unexpected token on react component

Jest encountered an unexpected token when testing a Vue single file component

Reactjs npm test: Jest encountered an unexpected token

Jest encountered an unexpected token UI-Kitten

How to fix Jest encountered an unexpected token

Using Jest within webpack 4 project throws 'Jest encountered an unexpected token' error

React Jest - Unexpected token import

How to get rid of SyntaxError: Unexpected Token { when trying to test a React-Native with a Mapbox in Jest?

Unexpected token import error in while running jest tests

Unexpected token 'import' error while running Jest tests?

Jest - Jest encountered an unexpected token. Vanilla JS

Jest Testing - Unexpected token Error

[React-Native][Jest]SyntaxError: Unexpected token import

React-native-modal - Unexpected Token with Jest using TransformIgnorePatterns

Jest encountered an unexpected token - SyntaxError: Unexpected token 'export'