【发布时间】:2020-07-21 22:48:05
【问题描述】:
我有一个基于 TypeScript 的 Vue 项目,使用 Jest 作为测试框架。我正在尝试测试的模块中有操作。
我的操作如下所示:
@Action({})
saveSomeData (payload: any): Promise<any> {
const { isActive, id, routes } = payload
return this.context.dispatch('otherModule/createId', '', { root: true })
.then((id: string) => {
payload = { isActive, id, routes, type: 'postRoutes' }
return this.context.dispatch('submitRoutes', payload)
})
}
@Action({})
submitRoutes (payload: any): Promise<any> {
const { isActive, id, routes, type } = payload
return ActiveService.setActive(id)
.then(() => this.context.dispatch(type, { id, routes }))
}
这是我的测试的样子:
// Mocking createId in otherModule module to return ID
jest.mock('@/store/modules/otherModule', () => ({
createId: jest.fn(() => Promise.resolve([
{
id: 'someId'
}
]))
}))
...
describe('Testing save MyModule data', () => {
let store: any
beforeEach(() => {
store = new Vuex.Store({
modules: {
myModule,
otherModule
}
})
})
test('Should call createId and then call submitRoutes if ID is empty', async () => {
const payload = {
isActive: true,
id: '',
routes: []
}
const pld = {
isActive: true,
id: 'someId',
routes: [],
type: 'postRoutes'
}
store.dispatch = jest.fn()
await store.dispatch('myModule/saveSomeData', payload)
expect(store.dispatch).toHaveBeenCalledWith('myModule/saveSomeData', payload)
expect(store.dispatch).toHaveBeenCalledWith('otherModule/createId') // <-- here I get an error
expect(store.dispatch).toHaveBeenCalledWith('myModule/submitRoutes', pld)
})
})
问题:我的测试失败了,我还没有找到任何方法让它工作。
错误:
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "otherModule/createId"
Received: "myModule/saveSomeData", {"id": "", "isActive": true, "routes": []}
Number of calls: 1
我的尝试
我已经关注了Vuex 和Jest 的文档,我还尝试了来自互联网的不同解决方案 - 不幸的是没有运气。
我将不胜感激。
【问题讨论】:
标签: javascript typescript vue.js jestjs vuex