How to test a React Native component that imports a custom native module with Jest?

后端 未结 5 755
陌清茗
陌清茗 2021-02-07 04:24

Here is a simple component that I am trying to test using React Native 0.39 and Jest 18:

// index.ios.js

import React, { Component } from \'react\';
import { Ap         


        
5条回答
  •  没有蜡笔的小新
    2021-02-07 05:02

    This way, you will mock it once (before jest starts)

    jest.config.js

    module.exports = {
      preset: 'react-native',
      setupFiles: ['./__mocks__/your-native-bridge.js']
    };
    

    __mocks__/your-native-bridge.js

    import {NativeModules} from 'react-native';
    
    NativeModules.YourNativeBridge = {
      property: jest.fn()
    };
    

    Don't forget to mock all possible functions, properties in YourNativeBridge

提交回复
热议问题