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
Copy file name to clipboardExpand all lines: packages/docs/cookbook/testing.md
+38-3Lines changed: 38 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ beforeEach(() => {
66
66
67
67
## Unit testing components
68
68
69
-
This can be achieved with `createTestingPinia()`. I haven't been able to write proper documentation for this yet but its usage can be discovered through autocompletion and the documentation that appears in tooltips.
69
+
This can be achieved with `createTestingPinia()`, which returns a pinia instance designed to help unit tests components.
70
70
71
71
Start by installing `@pinia/testing`:
72
72
@@ -94,8 +94,8 @@ store.name = 'my new name'
94
94
store.$patch({ name:'new name' })
95
95
expect(store.name).toBe('new name')
96
96
97
-
// actions are stubbed by default but can be configured by
98
-
//passing an option to `createTestingPinia()`
97
+
// actions are stubbed by default, meaning they don't execute their code by default.
// Now this call WILL execute the implementation defined by the store
124
+
store.someAction()
125
+
126
+
// ...but it's still wrapped with a spy, so you can inspect calls
127
+
expect(store.someAction).toHaveBeenCalledTimes(1)
128
+
129
+
```
130
+
131
+
### Specifying the createSpy function
132
+
133
+
When using Jest, or vitest with `globals: true`, `createTestingPinia` automatically stubs actions using the spy function based on the existing test framework (`jest.fn` or `vitest.fn`). If you are using a different framework, you'll need to provide a [createSpy](/api/interfaces/pinia_testing.testingoptions.html#createspy) option:
134
+
135
+
```js
136
+
importsinonfrom'sinon'
137
+
138
+
createTestingPinia({
139
+
createSpy:sinon.spy// use sinon's spy to wrap actions
140
+
})
141
+
107
142
You can find more examples in [the tests of the testing package](https://github.com/vuejs/pinia/blob/v2/packages/testing/src/testing.spec.ts).
0 commit comments