vue.js - vue 3 pinia testing - spying and asserting on actions - Stack Overflow

I have the following simplified pinia store:export const useMyStore = defineStore('myStore',

I have the following simplified pinia store:

export const useMyStore = defineStore('myStore', () => {
  const doSomething = (param) => {
    console.log('doSomething called with: ', param)
    setData(param)
  }

  const setData = (data) => {
    console.log('setData called with: ', data)
  }

  return {
    doSomething, setData
  }
})

In my test file (vitest), I invoke doSomething(), which in turn will call setData(). I want to assert that setData() was indeed called.

describe('myStore', () => {
  test('doSomething()', () => {
    const pinia = createPinia()
    setActivePinia(pinia)
    const store = useMyStore()
    const spy = vi.spyOn(store, 'setData')

    const data = {
      name: 'doge'
    }

    store.doSomething(data)
    expect(spy).toHaveBeenCalledOnce()
  })
})

However, the above does not work. I can see the console.log messages in the output, but the assertion fails: AssertionError: expected "wrappedAction" to be called once, but got 0 times

I have also tried mocking, but it does not work either.

    const mock = vi.fn()
    store.setData = mock
    store.doSomething(data)
    expect(mock).toHaveBeenCalledOnce()

Is there a way to simply assert that setData() was called?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098960a4611156.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信