简体   繁体   English

React Native jest test:TypeError:无法读取未定义的属性'unsubscribeFromTopic'

[英]React Native jest test: TypeError: Cannot read property 'unsubscribeFromTopic' of undefined

I'm using react-native-fcm and jest to test my React Native app. 我正在使用react-native-fcmjest来测试我的React Native应用程序。 I have a pretty basic test, it looks like this: 我有一个非常基本的测试,它看起来像这样:

import 'react-native';
import React from 'react';
import PushController from '../app/PushController';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('works correctly', () => {
  const tree = renderer.create(
    <PushController />
  );
});

And PushController is somewhat large, so here's the interesting parts PushController有点大,所以这里是有趣的部分

import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
import FCM from 'react-native-fcm';

export default class PushController extends Component {
(...)

  componentDidMount() {
    if (this.notificationListener) this.notificationListener.remove();

    this.notificationListener = FCM.on('notification', (notif) => {

        if (!notif.local_notification) {
          this.notifyUser(notif.coffee);
        }

    });
    FCM.unsubscribeFromTopic('/topics/coffee');
    FCM.subscribeToTopic('/topics/coffee');
  }
(...)

However, when running the test I get 但是,在运行测试时,我得到了

__tests__/PushControllerTest.js
  ● works correctly

    TypeError: Cannot read property 'unsubscribeFromTopic' of undefined

      at Object.FCM.unsubscribeFromTopic (node_modules/react-native-fcm/index.js:86:15)
      at PushController.componentDidMount (app/PushController.js:44:26)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.ON_DOM_READY_QUEUEING.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.TransactionImpl.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
      at ReactTestReconcileTransaction.TransactionImpl.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)

I've tried including lots of stuff in the test, like jest.mock('react-native-fcm') and others, but I can't get it to work at all. 我试过在测试中包含很多东西,比如jest.mock('react-native-fcm')和其他东西,但我根本无法让它工作。 I get that jest automatically mocks the library, but I don't understand why FCM is undefined. 我得到那个jest自动嘲笑库,但我不明白为什么FCM是未定义的。 Any ideas? 有任何想法吗?

I managed to solve it, finally! 我终于设法解决了! Simply needed to change my test to 只需要将我的测试改为

import 'react-native';
import React from 'react';
import PushController from '../app/PushController';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
import FCM from 'react-native-fcm'; // <-- This

it('works correctly', () => {
  FCM.unsubscribeFromTopic = jest.fn(); // <-- These two 
  FCM.subscribeToTopic = jest.fn();
  const tree = renderer.create(
    <PushController />
  );
});

To make sure the actual calls are mocked. 确保实际的通话被嘲笑。 I did a lot of googling before this, so I'm sure this will be useful for someone. 在此之前我做了很多谷歌搜索,所以我相信这对某些人有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法在玩笑测试中读取未定义的属性“原型” - TypeError: Cannot read property 'prototype' of undefined in jest test Jest - 测试给出错误类型错误:无法读取未定义的属性“then” - Jest - Test gives an error TypeError: Cannot read property 'then' of undefined 尝试在 React Native 上运行 Jest,出现多个错误,最后一个是:TypeError: Cannot read property 'createAnimatedComponent' of undefined - Trying to run Jest on React Native, multiple Errors occur, last one is: TypeError: Cannot read property 'createAnimatedComponent' of undefined React Jest:TypeError:无法读取未定义的属性“地图” - React Jest: TypeError: Cannot read property 'map' of undefined 类型错误:无法读取 ReactJS Jest 测试中未定义的属性“标题” - TypeError: Cannot read property 'title' of undefined in ReactJS Jest Test Angular 测试笑话 - 类型错误:无法读取未定义的属性“queryParams” - Angular Test Jest - TypeError: Cannot read property 'queryParams' of undefined 开玩笑 Nuxt 测试类型错误:无法读取未定义的属性“$get” - Jest Nuxt Test TypeError: Cannot read property '$get' of undefined React - 使用 Jest 进行测试 - TypeError:无法读取未定义的属性“销毁” - React - Testing w/ Jest - TypeError: Cannot read property 'destroy' of undefined 玩笑/反应-类型错误:无法读取未定义的属性“touchStart” - Jest/react - TypeError: Cannot read property 'touchStart' of undefined Jest - TypeError:无法读取未定义的属性“fn” - Jest - TypeError: Cannot read property 'fn' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM