Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Given a TestingPinia instance of a store with a watch, the watch actions are not stubbed #2291

Closed
cadriel opened this issue Jun 29, 2023 · 2 comments

Comments

@cadriel
Copy link

cadriel commented Jun 29, 2023

Reproduction & Steps to reproduce the bug

Given a composable store that has a watch - a TestingPinia instance of that store will run the actions as if they were not stubbed. Actions outside of the watch are stubbed as expected.

# A real store..
export const useSomeStore = defineStore('somestore', () => {
  const someId = ref(10)

  watch(
    someId,
    () => { someAction() },
    { immediate: true }
  )

  async function someAction() {
    try {
      // Something here, like an API call.
    } catch (e) {
      console.error(e)
    }
  }
})
# A spec..
it('should pass', async () => {
  const pinia = createTestingPinia({ initialState, stubActions: true })
  const wrapper = mount(someComponent, {
    global: {
      plugins: [pinia, ...]
    }
  })

  // If the initial state has someId defined, or if you set someId .. the action is run unstubbed.
})

Expected behavior

The action to have been stubbed and not run...

Actual behavior

The original, unstubbed action is run.

Additional information

It took me a while to figure out why I was getting a bunch of API calls making their way through until I realized the actual actions were being run from the watch.

@cadriel
Copy link
Author

cadriel commented Jun 29, 2023

Also, I noticed this isn't immediately apparent until you fire flushPromises enough times that the watch is hit.

Copy link
Member

posva commented Jun 30, 2023

Unfortunately it is simply not possible to stub a function this way. It would be great to document this. Feel free to open a PR!

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
@posva posva mentioned this issue May 4, 2023
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants