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
feat(testing): add selective action stubbing support
- Add support for include/exclude options in stubActions parameter
- Allow stubbing only specific actions or excluding specific actions from stubbing
- Maintain backward compatibility with boolean stubActions values
- Add comprehensive tests for all selective stubbing scenarios
- Update documentation with examples and usage patterns
- Fix workspace naming conflict in online-playground package
Closes#2970
Copy file name to clipboardExpand all lines: packages/docs/cookbook/testing.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -166,6 +166,70 @@ store.someAction()
166
166
expect(store.someAction).toHaveBeenCalledTimes(1)
167
167
```
168
168
169
+
### Selective action stubbing
170
+
171
+
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:
172
+
173
+
```js
174
+
// Only stub the 'increment' and 'reset' actions
175
+
constwrapper=mount(Counter, {
176
+
global: {
177
+
plugins: [
178
+
createTestingPinia({
179
+
stubActions: { include: ['increment', 'reset'] }
180
+
})
181
+
],
182
+
},
183
+
})
184
+
185
+
conststore=useSomeStore()
186
+
187
+
// These actions will be stubbed (not executed)
188
+
store.increment() // stubbed
189
+
store.reset() // stubbed
190
+
191
+
// Other actions will execute normally but still be spied
192
+
store.fetchData() // executed normally
193
+
expect(store.fetchData).toHaveBeenCalledTimes(1)
194
+
```
195
+
196
+
Alternatively, you can exclude specific actions from stubbing:
197
+
198
+
```js
199
+
// Stub all actions except 'fetchData'
200
+
constwrapper=mount(Counter, {
201
+
global: {
202
+
plugins: [
203
+
createTestingPinia({
204
+
stubActions: { exclude: ['fetchData'] }
205
+
})
206
+
],
207
+
},
208
+
})
209
+
210
+
conststore=useSomeStore()
211
+
212
+
// This action will execute normally
213
+
store.fetchData() // executed normally
214
+
215
+
// Other actions will be stubbed
216
+
store.increment() // stubbed
217
+
store.reset() // stubbed
218
+
```
219
+
220
+
::: 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`).
222
+
:::
223
+
224
+
You can also manually mock specific actions after creating the store:
Actions are automatically spied but type-wise, they are still the regular actions. In order to get the correct type, we must implement a custom type-wrapper that applies the `Mock` type to each action. **This type depends on the testing framework you are using**. Here is an example with Vitest:
0 commit comments