From: Eduardo San Martin Morote Date: Sun, 29 Oct 2023 09:16:33 +0000 (+0100) Subject: docs: example for vitest X-Git-Tag: @pinia/nuxt@0.5.2-beta.0~103 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=40f8f2252daa6cbc5e9815c63ed44dbf5127dc61;p=thirdparty%2Fvuejs%2Fpinia.git docs: example for vitest See https://github.com/vuejs/pinia/discussions/2464 --- diff --git a/packages/docs/cookbook/testing.md b/packages/docs/cookbook/testing.md index d125daa8..29261eb6 100644 --- a/packages/docs/cookbook/testing.md +++ b/packages/docs/cookbook/testing.md @@ -157,16 +157,29 @@ expect(store.someAction).toHaveBeenCalledTimes(1) ### Specifying the createSpy function -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: +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 not using `globals: true` or using a different framework, you'll need to provide a [createSpy](/api/interfaces/pinia_testing.TestingOptions.html#createspy) option: -```js +::: code-group + +```ts [vitest] +// NOTE: not needed with `globals: true` +import { vi } from 'vitest' + +createTestingPinia({ + createSpy: vi.fn, +}) +``` + +```ts [sinon] import sinon from 'sinon' createTestingPinia({ - createSpy: sinon.spy, // use sinon's spy to wrap actions + createSpy: sinon.spy, }) ``` +::: + 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). ### Mocking getters