如何在Jest中模拟React Native本机模块?

时间:2019-04-26 21:21:59

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

我想要类似以下内容(实际上有效...):

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模拟的基础上构建。

1 个答案:

答案 0 :(得分:0)

您可以尝试直接模拟NativeModules

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