You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes you may want to stub only specific actions while allowing others to execute normally. You can achieve this by passing an object with `include` or `exclude` arrays to the `stubActions` option:
171
+
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:
Alternatively, you can exclude specific actions from stubbing:
196
+
For more complex scenarios, you can pass a function that receives the action name and store instance, and returns whether the action should be stubbed:
197
197
198
198
```js
199
-
// Stub all actions except 'fetchData'
199
+
// Stub actions based on custom logic
200
200
constwrapper=mount(Counter, {
201
201
global: {
202
202
plugins: [
203
203
createTestingPinia({
204
-
stubActions: { exclude: ['fetchData'] }
204
+
stubActions: (actionName, store) => {
205
+
// Stub all actions that start with 'set'
206
+
if (actionName.startsWith('set')) returntrue
207
+
208
+
// Stub actions based on initial store state
209
+
if (store.isPremium) returnfalse
210
+
211
+
returntrue
212
+
}
205
213
})
206
214
],
207
215
},
208
216
})
209
217
210
218
conststore=useSomeStore()
211
219
212
-
//This action will execute normally
213
-
store.fetchData() //executed normally
220
+
//Actions starting with 'set' are stubbed
221
+
store.setValue(42) //stubbed
214
222
215
-
// Other actions will be stubbed
216
-
store.increment() // stubbed
217
-
store.reset() // stubbed
223
+
// Other actions may execute based on the initial store state
224
+
store.fetchData() // executed or stubbed based on initial store.isPremium
218
225
```
219
226
220
227
::: tip
221
-
If both `include` and `exclude` are provided, `include` takes precedence. If neither is provided or both arrays are empty, all actions will be stubbed (equivalent to `stubActions: true`).
228
+
- An empty array `[]` means no actions will be stubbed (all actions execute normally)
229
+
- The function is evaluated once at store setup time, receiving the store instance in its initial state
222
230
:::
223
231
224
232
You can also manually mock specific actions after creating the store:
0 commit comments