测试在不同文件中具有依赖关系的模块时.将该模块指定jest.Mock
为mockReturnThisOnce
typescript时会给出一个错误,即该依赖项上不存在该方法(或任何其他jest.Mock方法),这是因为它以前是键入的.从jest.Mock获取typescript继承类型的正确方法是什么?
这是一个简单的例子.
依赖
const myDep = (name: string) => name;
export default myDep;
Run Code Online (Sandbox Code Playgroud)
test.ts
import * as dep from '../depenendency';
jest.mock('../dependency');
it('should do what I need', () => {
//this throws ts error
// Property mockReturnValueOnce does not exist on type (name: string)....
dep.default.mockReturnValueOnce('return')
}
Run Code Online (Sandbox Code Playgroud)
我觉得这是一个非常常见的用例,不知道如何正确输入.任何帮助将非常感激!
所以我正在开发一个大型的react/redux应用程序,并且非常满意它在redux中管理状态的简单程度.我们正在使用"ducks"风格的方法来减少/动作/动作创建者以保持相对基于域的东西.此外,我们尝试保持ui状态与域相关联,并且大多数reducer具有类似的结构.它看起来像这样:
export default function home(state = initialState, action = {}) {
switch (action.type) {
case OPEN:
return {
...state,
ui: {
...state.ui,
[action.key]: true
}
};
case CLOSE:
return {
...state,
ui: {
...state.ui,
[action.key]: false
}
};
case SELECT_TAB:
return {
...state,
ui: {
selectedTab: action.selected
}
};
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
我们最终会为ui反复重复相同的操作,主要是切换和设置显示的内容.有没有办法将基于域的ui保留在reducer中,而无需在每个reducer中为ui 添加OPEN
和CLOSE
类型语句.也许我正在考虑的是redux中的反模式,但很想知道之前是否有人遇到过这类问题?
更新:
我喜欢下面列出的解决方案,但是如何将reducer扩展为包含ui reducer的类型.
combineReducers({
home: {createUiReducer('home', initialState), ...home}
})
Run Code Online (Sandbox Code Playgroud)
这样,您可以使用基于嵌套域的ui而无需重复所有操作.不知道你会怎么做,因为你基本上是动态添加CASE语句.