【问题标题】:How to mock a React Native native module"in Jest?如何在 Jest 中模拟 React Native 原生模块?
【发布时间】:2019-04-29 22:16:08
【问题描述】:

我想要类似以下的东西(实际上有效......):

import { NativeModules } from 'react-native';
import PushNotifications from '../../app/platform/PushNotificationSupport';


const mockRNA = jest.requireMock('react-native');
jest.mock('react-native', () => {
    return {
        default: mockRNA.default,
        NativeModules: {
            ...mockRNA.NativeModules,
            NativePushNotifications: {
                setTokenHandler: jest.fn(),
            },
        },
    };
});

当然,上面的代码实际上并不起作用。我基本上想在现有的react-native mock 之上构建。

【问题讨论】:

    标签: react-native jestjs react-native-native-module


    【解决方案1】:

    你可以尝试直接模拟NativeModules

    jest.mock('NativeModules', () => ({
        NativePushNotifications: {
            setTokenHandler: jest.fn(),
        },
        ...
    }));
    

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2017-06-08
      • 2019-12-26
      • 2017-02-23
      • 1970-01-01
      相关资源
      最近更新 更多