在带有 redux-mock-store 的模块中使用 Jest 模拟 redux-store getState() [重复]

问题描述 投票:0回答:1

所以我对 redux-store 的实现可能不是最好的,而且我对 Jest 的了解也是最少的..

我有三个文件,其中包括...

  1. 一个模块
  2. React 组件
  3. (普通)商店,默认导出

我的测试文件如下所示:

// file.test.js


jest.mock( 'store', () => jest.fn() )

// external
import React from 'react'
import configureStore from 'redux-mock-store'

// internal
import Component from 'components/Component'
import store from 'store'

describe( 'Test My Component', () => {
    const mockStore = configureStore()

    it ( 'should equal true', () => {
        const initialState = { active: true }

        store.mockImplementation(() => mockStore( initialState ))

        expect( store.getState().active ).toBe( true )
    } )
} );

我嘲笑商店的原因是因为在

<Component />
内部我使用的模块本身导入相同的
store
并拥有一些正在使用
store.getState()
...

的函数

所以返回的是

TypeError: _store2.default.getState is not a function

我确实通过模拟模块调用(由

<Component />
使用)来解决这个问题,但我觉得我“应该”能够做到这一点,因为我有不同的初始状态,我想尝试而不是全部取决于模块。

这是我关于SO的第一篇文章,如果我需要澄清任何事情,请告诉我!

javascript unit-testing redux jestjs
1个回答
2
投票

不要导入商店。我们想嘲笑商店:

const store = mockStore(initialState);

© www.soinside.com 2019 - 2024. All rights reserved.