jest config is throwing "type ErrorHandler = (error: mixed, isFatal: boolean) => void" after update to 26.x
Asked Answered
A

4

38

i don't know why this is suddenly not working but this is my jest.config.js:

module.exports = {
  preset: 'react-native',
  verbose: true,
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  setupFiles: ['./jestSetup.js'],
  transformIgnorePatterns: [
    'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
  ],
  moduleNameMapper: {
    '.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
  },
};

and my jestSetup.js:

import 'react-native-gesture-handler/jestSetup';
import '@testing-library/jest-native/extend-expect';
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

beforeAll(() => {
  //@ts-ignore
  global.__reanimatedWorkletInit = jest.fn();
});

jest.mock('react-native-share', () => ({
  default: jest.fn(),
}));

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

jest.mock('react-native-reanimated', () => {
  const Reanimated = require('react-native-reanimated/mock');

  Reanimated.default.call = () => {};

  Reanimated.useSharedValue = jest.fn;
  Reanimated.useAnimatedStyle = jest.fn;
  return Reanimated;
});

jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

jest.mock('react-native-gesture-handler', () =>
  jest.requireActual('../node_modules/react-native-gesture-handler/jestSetup')
);

jest.mock('react-native-intercom', () => {}, { virtual: true });

jest.mock('@react-native-async-storage/async-storage', () =>
  require('@react-native-async-storage/async-storage/jest/async-storage-mock')
);

jest.mock('react-native-geolocation-service', () => ({
  addListener: jest.fn(),
  getCurrentPosition: jest.fn(),
  removeListeners: jest.fn(),
  requestAuthorization: jest.fn(),
  setConfiguration: jest.fn(),
  startObserving: jest.fn(),
  stopObserving: jest.fn(),
}));

export const mockedNavigate = jest.fn();

jest.mock('@react-navigation/native', () => ({
  ...jest.requireActual('@react-navigation/native'),
  useNavigation: () => ({
    navigate: mockedNavigate,
    goBack: jest.fn(),
  }),
  useRoute: () => ({
    params: {
      publicToken: 'testToken',
    },
  }),
}));

jest.mock('react-native-safe-area-context', () => {
  const React = require('react');
  class MockSafeAreaProvider extends React.Component {
    render() {
      const { children } = this.props;
      return React.createElement('SafeAreaProvider', this.props, children);
    }
  }
  return {
    useSafeAreaInsets: () => ({ top: 1, right: 2, bottom: 3, left: 4 }),
    SafeAreaProvider: MockSafeAreaProvider,
  };
});

and my babel.config.js:

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-typescript',
  ],
  plugins: [
    'react-native-reanimated/plugin',
    ['relay', { artifactDirectory: './src/__generated__' }],
    [
      'transform-es2015-modules-commonjs',
      {
        allowTopLevelThis: true,
      },
    ],
  ],
};

i have already looked at properly on the docs and can't figure out how this suddenly fails, below is the full error message:

node_modules/@react-native/polyfills/error-guard.js:14
    type ErrorHandler = (error: mixed, isFatal: boolean) => void;
         ^^^^^^^^^^^^

    SyntaxError: Unexpected identifier

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:453:6)
Anglesey answered 16/3, 2021 at 9:29 Comment(5)
That's a .js file that apparently contains a TypeScript type definition, that doesn't seem right.Tiffin
i tried everything, prior to it, it's tsAnglesey
Looking at the source, it's a Flow type alias.Tiffin
my react code is on ts, setup includes js filesAnglesey
This just started happening to me too. It worked fine a couple of days ago.Lait
L
79

Solution found here. Add @react-native to your Jest configuration. Such as:

transformIgnorePatterns: [
  'node_modules/(?!@react-native|react-native)'
],
Lait answered 16/3, 2021 at 23:9 Comment(2)
Mine looks like this: 'node_modules/(?!(jest-)?react-native|react-clone-referenced-element/*)' Hơw should I add @react-native to the string?Madonia
'node_modules/(?!(jest-)?@react-native|react-native|react-clone-referenced-element/*)'Lait
R
11

I needed an extra ? after the @ in the regex compared to the other solutions I found for this issue. Specifically, this is what I needed to add to my Jest configuration:

transformIgnorePatterns: [
  'node_modules/(?!(jest-)?@?react-native|@react-native-community|@react-navigation)'
],

Inspired by: https://github.com/callstack/react-native-testing-library/issues/703

Retrace answered 12/1, 2022 at 22:5 Comment(1)
This is the same as @matt-raible answer, but you're just making the @ optional with the ?Aeromancy
L
0

What worked for me was downgrading jest packages from 28.0.2 to 27.5.1

jest packages means:

  • jest
  • jest-cli
  • jest-circus
  • jest-environment-node
Lyublin answered 30/5, 2022 at 9:54 Comment(0)
P
0

Create a file jest.config.js and add this: (this avoid the major errors)

    /** @type {import('jest').Config} */
const config = {
  verbose: true,
  preset: 'react-native',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  transformIgnorePatterns: [
    'node_modules/(?!(react-native' +
      '|@react-native' +
      '|@react-native-community' +
      '|@react-navigation' +
      '|react-navigation-tabs' +
      '|react-native-splash-screen' +
      '|react-native-screens' +
      '|react-native-reanimated' +
      ')/)',
  ],
}

module.exports = config

If you created a config inside the package.json, remove it

Paulenepauletta answered 24/7, 2023 at 21:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.