当使用redux测试异步动作创建者并做出反应时,无法读取未定义的属性'.then'

int*_*der 17 reactjs jestjs redux redux-mock-store

我正在尝试使用react,redux-mock-store和redux编写一些测试,但我不断得到并且错误.也许是因为我Promise尚未解决?

fetchListing()当我在开发和生产上尝试它时,动作创建者实际上工作,但是我在测试通过时遇到了问题.

错误信息

(node:19143) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): SyntaxError
(node:19143) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 FAIL  src/actions/__tests__/action.test.js
  ? async actions › creates "FETCH_LISTINGS" when fetching listing has been done

    TypeError: Cannot read property 'then' of undefined

      at Object.<anonymous> (src/actions/__tests__/action.test.js:44:51)
          at Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:169:7)

  async actions
    ? creates "FETCH_LISTINGS" when fetching listing has been done (10ms)
Run Code Online (Sandbox Code Playgroud)

动作/ index.js

// actions/index.js
import axios from 'axios';

import { FETCH_LISTINGS } from './types';

export function fetchListings() {

  const request = axios.get('/5/index.cfm?event=stream:listings');

  return (dispatch) => {
    request.then(( { data } ) => {
      dispatch({ type: FETCH_LISTINGS, payload: data });
    });
  }
};
Run Code Online (Sandbox Code Playgroud)

action.test.js

// actions/__test__/action.test.js

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { applyMiddleware } from 'redux';
import nock from 'nock';
import expect from 'expect';

import * as actions from '../index';
import * as types from '../types';


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

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


it('creates "FETCH_LISTINGS" when fetching listing has been done', () => {
  nock('http://example.com/')
    .get('/listings')
    .reply(200, { body: { listings: [{ 'corpo_id': 5629, id: 1382796, name: 'masm' }] } })

    const expectedActions = [
      { type: types.FETCH_LISTINGS }, { body: { listings: [{ 'corpo_id': 5629, id: 1382796, name: 'masm' }] }}
    ]

    const store = mockStore({ listings: [] })

    return store.dispatch(actions.fetchListings()).then((data) => {
      expect(store.getActions()).toEqual(expectedActions)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Lew*_*ond 16

store.dispatch(actions.fetchListings())回报undefined.你不能打电话.then给那个.

请参阅redux-thunk代码.它执行你返回的函数并返回它.您返回的函数不fetchListings返回任何内容,即undefined.

尝试

return (dispatch) => {
    return request.then( (data) => {
      dispatch({ type: FETCH_LISTINGS, payload: data });
    });
  }
Run Code Online (Sandbox Code Playgroud)

之后你还会遇到另一个问题.你不会在你的内部归还任何东西then,你只需要发送.这意味着下一个then获得undefined参数


小智 8

我也知道这是一个旧线程,但是您需要确保在thunk中返回异步动作。

在我的重击中,我需要:

return fetch()
Run Code Online (Sandbox Code Playgroud)

异步动作,它起作用了