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
Please note that if you are using Vue 2, `@vue/test-utils` requires a [slightly different configuration](#unit-test-components-vue-2).
106
109
110
+
### Initial State
111
+
112
+
You can set the initial state of **all of your stores** when creating a testing pinia by passing an `initialState` object. This object will be used by the testing pinia to _patch_ stores when they are created. Let's say you want to initialize the state of this store:
113
+
114
+
```ts
115
+
import { defineStore } from'pinia'
116
+
117
+
const useCounterStore =defineStore('counter', {
118
+
state: () => ({ n: 0 }),
119
+
// ...
120
+
})
121
+
```
122
+
123
+
Since the store is named _"counter"_, you need to add a matching object to `initialState`:
124
+
125
+
```ts
126
+
// somewhere in your test
127
+
const wrapper =mount(Counter, {
128
+
global: {
129
+
plugins: [createTestingPinia(
130
+
131
+
initialState: {
132
+
counter: { n: 20 }, // start the counter at 20 instead of 0
133
+
},
134
+
)],
135
+
},
136
+
})
137
+
138
+
const store =useSomeStore() // uses the testing pinia!
139
+
store.n// 20
140
+
```
107
141
108
142
### Customizing behavior of actions
109
143
@@ -123,9 +157,8 @@ const store = useSomeStore()
123
157
// Now this call WILL execute the implementation defined by the store
124
158
store.someAction()
125
159
126
-
// ...but it's still wrapped with a spy, so you can inspect calls
160
+
// ...but it's still wrapped with a spy, so you can inspect calls
127
161
expect(store.someAction).toHaveBeenCalledTimes(1)
128
-
129
162
```
130
163
131
164
### Specifying the createSpy function
@@ -135,9 +168,10 @@ When using Jest, or vitest with `globals: true`, `createTestingPinia` automatica
135
168
```js
136
169
importsinonfrom'sinon'
137
170
138
-
createTestingPinia({
139
-
createSpy:sinon.spy// use sinon's spy to wrap actions
171
+
createTestingPinia({
172
+
createSpy:sinon.spy,// use sinon's spy to wrap actions
140
173
})
174
+
```
141
175
142
176
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