简体   繁体   English

Jest - 测试给出错误类型错误:无法读取未定义的属性“then”

[英]Jest - Test gives an error TypeError: Cannot read property 'then' of undefined

When i the run test it gives me an error TypeError: Cannot read property 'then' of undefined, I've tried to look for some fix on the Docs but i didn't find one to fix this issue, any idea on how to fix this problem will be much appreciated thanks.当我运行测试时,它给了我一个错误 TypeError: Cannot read property 'then' of undefined,我试图在文档上寻找一些修复,但我没有找到解决这个问题的方法,任何想法解决这个问题将不胜感激,谢谢。

contact-actions.js联系-actions.js

 export function getContacts() {
      return function(dispatch) {
        dispatch({type: 'GET_CONTACTS_PENDING'})
        axios.get('http://127.0.0.1:8000/api/contacts', { 
        })
          .then((response) => {
            dispatch({type: 'GET_CONTACTS', payload: response.data})
            dispatch(hideLoading())
          })
          .catch((error) => {
            dispatch({type:  'CONTACT_ERROR', payload: error})
        })
      }
    }


    app.spec.js

    import React from 'react';
    import { shallow, mount } from 'enzyme';
    import renderer from 'react-test-renderer';
    import MockAdapter from 'axios-mock-adapter';
    import configureMockStore from 'redux-mock-store';
    import nock from 'nock';
    import axios from 'axios';
    import moxios from 'moxios';
    import expect from 'expect';
    import thunk from 'redux-thunk';
    import { Provider } from 'react-redux';
    import * as actions from '../src/actions/contact-actions';
    import * as types from '../src/constants/action-types';

    const middlewares = [thunk];
    const mockStore = configureMockStore(middlewares);

    describe('async actions', () => {
      afterEach(() => {
        nock.cleanAll();
      });

      it('creates GET_CONTACTS when fetching contacts has been done', () => {
        const store = mockStore({});

        const contacts = {};

        nock('http://127.0.0.1:8000')
          .get('/api/contacts')
          .reply(200, { body: {  contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: 'shadow@yahoo.com' }] } });

        const expectedActions = [
          { type: types.GET_CONTACTS, body: {  contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: 'shadow@yahoo.com' }] }}
        ];

        return store.dispatch(actions.getContacts()).then(() => {
          // return of async actions
          expect(store.getActions()).toEqual(expectedActions);
        });
      });
    });

Be sure to return the promise from the anonymous function inside getContacts():确保从 getContacts() 中的匿名函数返回承诺:

export function getContacts() {
  return function(dispatch) {
    dispatch({type: 'GET_CONTACTS_PENDING'})
    axios.get('http://127.0.0.1:8000/api/contacts', { // THIS LINE
    })
    ...

Change this:改变这个:

axios.get('http://127.0.0.1:8000/api/contacts', {

to this:对此:

return axios.get('http://127.0.0.1:8000/api/contacts', {

Right now it lacks a return statement and so returns undefined .现在它缺少 return 语句,因此返回undefined

暂无
暂无

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

相关问题 类型错误:无法在玩笑测试中读取未定义的属性“原型” - TypeError: Cannot read property 'prototype' of undefined in jest test React Native jest test:TypeError:无法读取未定义的属性'unsubscribeFromTopic' - React Native jest test: TypeError: Cannot read property 'unsubscribeFromTopic' 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 开玩笑的错误:TypeError:无法读取未定义的属性“选项” - Jest error: TypeError: Cannot read property 'options' of undefined 开玩笑的错误” TypeError:无法读取未定义的属性'preventDefault' - Jest error '' TypeError: Cannot read property 'preventDefault' of undefined " 开玩笑:TypeError:无法读取未定义的属性“长度” - Jest : TypeError: Cannot read property 'length' of undefined TypeError:无法读取未定义的 Jest 和 Nest 的属性“then” - TypeError: Cannot read property 'then' of undefined Jest and Nest JEST - TypeError:无法读取未定义的属性“then” - JEST - TypeError: Cannot read property 'then' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM