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

后端 未结 5 773
陌清茗
陌清茗 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:04

    You can simply add a mock where your native module should be:

    import {
      NativeModules,
    } from 'react-native';
    import React from 'react';
    import renderer from 'react-test-renderer';
    
    describe('TestProject', () => {
      beforeEach(() => {
        NativeModules.TestModule = { test: jest.fn() } 
      });
      ...
    });
    

提交回复
热议问题