Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 297
gpbaculio Avatar asked Mar 16 '21 09:03

gpbaculio


1 Answers

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

transformIgnorePatterns: [
  'node_modules/(?!@react-native|react-native)'
],
like image 53
Matt Raible Avatar answered Oct 25 '22 18:10

Matt Raible