expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(0)
})
+
+ it('executes plugins', () => {
+ const { counter, wrapper } = factory({
+ plugins: [() => ({ pluginN: 0 })],
+ })
+
+ expect(counter.pluginN).toBe(0)
+ expect(wrapper.vm.counter.pluginN).toBe(0)
+ })
+
+ it('executes plugins with fakeApp', () => {
+ const pinia = createTestingPinia({
+ plugins: [() => ({ pluginN: 0 })],
+ fakeApp: true,
+ })
+
+ const counter = useCounter(pinia)
+
+ expect(counter.pluginN).toBe(0)
+ expect(pinia.app).toHaveProperty('mount', expect.any(Function))
+ })
})
+import { App, createApp } from 'vue'
import { createPinia } from './createPinia'
import { Pinia, PiniaStorePlugin, setActivePinia } from './rootStore'
export interface TestingOptions {
/**
- * Plugins to be installed before the testing plugin.
+ * Plugins to be installed before the testing plugin. Add any plugins used in
+ * your application that will be used while testing.
*/
plugins?: PiniaStorePlugin[]
*/
stubPatch?: boolean
+ /**
+ * Creates an empty App and calls `app.use(pinia)` with the created testing
+ * pinia. This is allows you to use plugins while unit testing stores as
+ * plugins **will wait for pinia to be installed in order to be executed**.
+ * Defaults to false.
+ */
+ fakeApp?: boolean
+
+ /**
+ * Function used to create a spy for actions and `$patch()`. Pre-configured
+ * with `jest.fn()` in jest projects.
+ */
createSpy?: (fn?: (...args: any[]) => any) => (...args: any[]) => any
}
* Clears the cache of spies used for actions.
*/
resetSpyCache(): void
+
+ /** App used by Pinia */
+ app: App
}
/**
plugins = [],
stubActions = true,
stubPatch = false,
+ fakeApp = false,
createSpy,
}: TestingOptions = {}): TestingPinia {
const pinia = createPinia()
store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch)
})
+ if (fakeApp) {
+ const app = createApp({})
+ app.use(pinia)
+ }
+
setActivePinia(pinia)
return Object.assign(
resetSpyCache() {
spiedActions.clear()
},
+ get app() {
+ return this._a as App
+ },
},
pinia
)