gpt4 book ai didi

reactjs - 如何用 Jest 模拟第三方 react 原生组件?

转载 作者:行者123 更新时间:2023-12-02 12:01:25 25 4
gpt4 key购买 nike

我正在使用NumericInput当我在我的设备上运行该应用程序时,它工作正常。

但是,当我运行 jest 时,我收到各种错误:

TypeError: Cannot read property 'default' of undefined

at new Icon (node_modules/react-native-vector-icons/lib/create-icon-set.js:42:389)
at constructClassInstance (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3435:18)
at updateClassComponent (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6606:5)
at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7563:16)
at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11234:12)
at workLoop (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11266:24)
at renderRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11349:7)
at performWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12237:7)
at performWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12149:7)
at performSyncWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12123:3)

console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9036
The above error occurred in the <Icon> component:
in Icon (at NumericInput.js:226)
in View (created by View)
in View (at createAnimatedComponent.js:151)
in AnimatedComponent (at TouchableOpacity.js:282)
in TouchableOpacity (at Button.js:18)
in Button (at NumericInput.js:225)
in View (created by View)
in View (at NumericInput.js:224)
in NumericInput
in View (created by View)
in View
in View (created by View)
in View (at ScrollViewMock.js:29)
in RCTScrollView (created by _class)
in _class (at ScrollViewMock.js:27)
in ScrollViewMock (created by App)
in App

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://.../react-error-boundaries to learn more about error boundaries.

第一个问题:这正常吗?

第二个问题:如果是,我该如何模拟 NumericInput

已关注 this guide ,看来我需要这样做:

jest.mock('react-native-numeric-input', () => 'NumericInput');

但是这不起作用。我也尝试过:

jest.mock('react-native-vector-icons', () => 'Icon');

没有成功。

o_0 这是怎么回事?

干杯!

最佳答案

这是 react-native 的官方 jest 预处理器的问题。

这是我的 Jest 配置文件:

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

为了解决这个问题,这是我的新 Jest 配置文件:

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

module.exports = {
preset: 'react-native',
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
moduleFileExtensions: [
'tsx',
...defaults.moduleFileExtensions
],
};

使用 'react-native' 预设时,您不需要 jest 预处理器转换项。 For more info .

关于reactjs - 如何用 Jest 模拟第三方 react 原生组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com