Skip to content

Commit 94c9c7b

Browse files
docs: add code exemple for Vue 2 unit tests (#1086)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 3c11a7e commit 94c9c7b

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

packages/docs/cookbook/testing.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Stores will, by design, be used at many places and can make testing much harder
88

99
Depending on what or how you are testing, we need to take care of these three differently:
1010

11-
- [Unit testing stores (outside of components)](#unit-testing-a-store)
12-
- [Unit testing components that use stores](#unit-testing-components)
13-
- [End to End tests](#e2e-tests)
11+
- [Testing stores](#testing-stores)
12+
- [Unit testing a store](#unit-testing-a-store)
13+
- [Unit testing components](#unit-testing-components)
14+
- [E2E tests](#e2e-tests)
15+
- [Unit test components (Vue 2)](#unit-test-components-vue-2)
1416

1517
## Unit testing a store
1618

@@ -100,8 +102,30 @@ expect(store.someAction).toHaveBeenCalledTimes(1)
100102
expect(store.someAction).toHaveBeenLastCalledWith()
101103
```
102104

105+
Please note that if you are using Vue 2, `@vue/test-utils` requires a [slightly different configuration](#unit-test-components-vue-2).
106+
103107
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).
104108

105109
## E2E tests
106110

107111
When it comes to pinia, you don't need to change anything for e2e tests, that's the whole point of e2e tests! You could maybe test HTTP requests, but that's way beyond the scope of this guide 😄.
112+
113+
## Unit test components (Vue 2)
114+
115+
When using [Vue Test Utils 1](https://v1.test-utils.vuejs.org/), install Pinia on a `localVue`:
116+
117+
```js
118+
import { PiniaVuePlugin } from 'pinia'
119+
import { createLocalVue, mount } from '@vue/test-utils'
120+
import { createTestingPinia } from '@pinia/testing'
121+
122+
const localVue = createLocalVue()
123+
localVue.use(PiniaVuePlugin)
124+
125+
const wrapper = mount(Counter, {
126+
localVue,
127+
pinia: createTestingPinia(),
128+
})
129+
130+
const store = useSomeStore() // uses the testing pinia!
131+
```

0 commit comments

Comments
 (0)