【问题标题】:Jest mock redux-store getState() used in module with redux-mock-storeJest mock redux-store getState() 在模块中使用 redux-mock-store
【发布时间】:2017-06-12 19:58:47
【问题描述】:

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

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

  1. 一个模块
  2. 一个反应组件
  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 jestjs redux-store


    【解决方案1】:

    不要导入商店。我们想模拟商店:

    const store = mockStore(initialState);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 2016-02-29
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多