gpt4 book ai didi

javascript - 如何在 Jest 测试中模拟 StatusBarManager.getHeight?

转载 作者:行者123 更新时间:2023-12-05 05:09:11 27 4
gpt4 key购买 nike

我正在使用 expo 34 和 react-native-ui-lib来自 wix,在为我的组件设置 Jest 测试时遇到问题。问题看起来出现在 link 中Wix 库的

function setStatusBarHeight() {
statusBarHeight = isIOS ? 20 : StatusBarManager.HEIGHT;
if (isIOS) {
// override guesstimate height with the actual height from StatusBarManager
StatusBarManager.getHeight(data => (statusBarHeight = data.height));
}
}

TypeError: StatusBarManager.getHeight 不是函数

TypeError: StatusBarManager.getHeight is not a function If I simply change it and return 42 my test can run.

有什么方法可对于Jest地模拟 StatusBarManager.getHeight 吗?

我尝试在我的根文件夹中创建 jest-setup.js

import { NativeModules } from 'react-native';

NativeModules.StatusBarManager = {getHeight: jest.fn()};

// mock native modules
jest.mock('@react-native-community/blur', () => {});

但是没有用。我当前的 jest.config.js

module.exports = {
preset: "jest-expo",
moduleFileExtensions: ['js','jsx','json', 'ts', 'tsx'],
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "babel-jest"
},
testMatch: [
"**/*.test.ts?(x)"
],
}

最佳答案

我能够用这段代码解决这个问题

import React from 'react'
import 'react-native'
import { NativeModules } from 'react-native' // !this module
import renderer from 'react-test-renderer'
import Description from './Description'

// and this mock
NativeModules.StatusBarManager = {getHeight: jest.fn()}

describe('TextArea test', () => {
it('Empty TextArea with title', () => {
const description = renderer.create(
<Description {...{

关于javascript - 如何在 Jest 测试中模拟 StatusBarManager.getHeight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755131/

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