jest config is throwing "type ErrorHandler = (error: mixed, isFatal: boolean) => void" after update to 26.x
jest config is throwing "type ErrorHandler = (error: mixed, isFatal: boolean) => void" after update to 26.x
我不知道为什么这突然不起作用,但这是我的 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)
找到解决方案 here。将 @react-native
添加到您的 Jest 配置中。如:
transformIgnorePatterns: [
'node_modules/(?!@react-native|react-native)'
],
与我为这个问题找到的其他解决方案相比,我在正则表达式的 @
之后需要一个额外的 ?
。具体来说,这是我需要添加到我的 Jest 配置中的内容:
transformIgnorePatterns: [
'node_modules/(?!(jest-)?@?react-native|@react-native-community|@react-navigation)'
],
灵感来自:https://github.com/callstack/react-native-testing-library/issues/703
对我有用的是将 jest 包从 28.0.2 降级到 27.5.1
jest 包意味着:
- 开玩笑
- jest-cli
- jest-circus
- jest-environment-node
我不知道为什么这突然不起作用,但这是我的 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)
找到解决方案 here。将 @react-native
添加到您的 Jest 配置中。如:
transformIgnorePatterns: [
'node_modules/(?!@react-native|react-native)'
],
与我为这个问题找到的其他解决方案相比,我在正则表达式的 @
之后需要一个额外的 ?
。具体来说,这是我需要添加到我的 Jest 配置中的内容:
transformIgnorePatterns: [
'node_modules/(?!(jest-)?@?react-native|@react-native-community|@react-navigation)'
],
灵感来自:https://github.com/callstack/react-native-testing-library/issues/703
对我有用的是将 jest 包从 28.0.2 降级到 27.5.1
jest 包意味着:
- 开玩笑
- jest-cli
- jest-circus
- jest-environment-node