Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/docs/cookbook/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,65 @@ store.someAction()
expect(store.someAction).toHaveBeenCalledTimes(1)
```

### Stubbing limitations in Setup stores

When using Setup stores (Composition API), internal calls between actions use closed-over function references rather than the store proxy. This means `stubActions` cannot intercept them, only external calls through the store instance are affected:

```js
// Setup store
export const useCounterStore = defineStore('counter', () => {
function increment() {
/*...*/
}

function incrementTwice() {
increment() // ❌ internal call β€” NOT intercepted by stubActions
increment()
}

return { increment, incrementTwice }
})

const store = useCounterStore()
store.increment() // βœ… stubbed β€” called through the store proxy
store.incrementTwice() // βœ… stubbed β€” but its internal call to increment() will still run
```
Comment thread
dennybiasiolli marked this conversation as resolved.

Options API stores do not have this limitation because they use `this` for internal calls, which always goes through the store proxy:

```js
// Options store
export const useCounterStore = defineStore('counter', {
actions: {
increment() {
/*...*/
},
incrementTwice() {
this.increment() // βœ… internal call β€” intercepted by stubActions
this.increment()
},
},
})
```

If you need to test internal calls in a Setup store, the workaround is to call actions via the store proxy (e.g. `useStore().increment()`) instead of directly calling the function. This way, they will be affected by `stubActions` as expected.

```js
// Setup store with workaround
export const useCounterStore = defineStore('counter', () => {
function increment() {
/*...*/
}

function incrementTwice() {
useCounterStore().increment() // βœ… call through the store proxy β€” will be stubbed
useCounterStore().increment()
}

return { increment, incrementTwice }
})
```

### Selective action stubbing

Sometimes you may want to stub only specific actions while allowing others to execute normally. You can achieve this by passing an array of action names to the `stubActions` option:
Expand Down