首页 > 解决方案 > 使用 Jest 和 Enzyme 在 Redux 中测试存储文件

问题描述

我们如何使用 Jest 和 Enzyme 编写商店创建文件的测试?

import {applyMiddleware, createStore} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';

const store = createStore(rootReducer, applyMiddleware(thunk))

export default store;

我试过这个,是否需要。

import store from "..";

describe("store", () => {
    it("should create a store", () => {
        expect(store).toEqual(
            expect.objectContaining({
                dispatch: expect.any(Function),
                getState: expect.any(Function),
            })
        );
    });
});

标签: reactjsreduxjestjsenzyme

解决方案


import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import fetchMock from 'fetch-mock'

const middlewares = [thunk]
// mocking store
const mockStore = configureMockStore(middlewares)

describe('some async actions', () => {
  afterEach(() => {
    fetchMock.restore()
  })
})

推荐阅读