jest config抛出“type ErrorHandler =(错误:混合,致命:boolean)=> void”after update to 26.x

hivapdat  于 2023-08-01  发布在  Jest
关注(0)|答案(4)|浏览(196)

我不知道为什么突然不工作了,但这是我的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',
  },
};

字符串
我的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,
  };
});


我的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,
      },
    ],
  ],
};


我已经看过了正确的文档,无法弄清楚这是如何突然失败的,下面是完整的错误消息:

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)

4jb9z9bj

4jb9z9bj1#

找到的解决方案here。将@react-native添加到Jest配置中。例如:

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

字符串

sg3maiej

sg3maiej2#

与我为这个问题找到的其他解决方案相比,我需要在正则表达式中的@之后添加一个?。具体来说,这是我需要添加到Jest配置中的内容:

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

字符串
灵感来源:https://github.com/callstack/react-native-testing-library/issues/703

q9yhzks0

q9yhzks03#

对我有效的是将jest包从28.0.2降级到27.5.1
jest packages是指:

  • 玩笑
  • jest-cli
  • 滑稽马戏团
  • jest-environment-node
zazmityj

zazmityj4#

创建一个jest.config.js文件,并添加以下内容:(避免出现重大错误)

/** @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

字符串
如果您在package.json中创建了配置,请将其删除

相关问题