小编I. *_*ota的帖子

如何在 Vue 应用程序中对嵌套的 Action 进行单元测试?

我有一个基于 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 }))
  }
Run Code Online (Sandbox Code Playgroud)

这是我的测试的样子:

// Mocking createId in otherModule module to …
Run Code Online (Sandbox Code Playgroud)

javascript typescript vue.js jestjs vuex

6
推荐指数
1
解决办法
894
查看次数

标签 统计

javascript ×1

jestjs ×1

typescript ×1

vue.js ×1

vuex ×1