diff --git a/packages/pinia/src/createPinia.ts b/packages/pinia/src/createPinia.ts index f71497fd31..fe2f0194c2 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -63,3 +63,18 @@ export function createPinia(): Pinia { return pinia } + +/** + * Dispose a Pinia instance by stopping its effectScope and removing the state, plugins and stores. This is mostly + * useful in tests, with both a testing pinia or a regular pinia and in applications that use multiple pinia instances. + * + * @param pinia - pinia instance + */ +export function disposePinia(pinia: Pinia) { + pinia._e.stop() + pinia._s.clear() + pinia._p.splice(0) + pinia.state.value = {} + // @ts-expect-error: non valid + pinia._a = null +} diff --git a/packages/pinia/src/index.ts b/packages/pinia/src/index.ts index fc28044f4d..d06aedc4fa 100644 --- a/packages/pinia/src/index.ts +++ b/packages/pinia/src/index.ts @@ -2,7 +2,7 @@ * @module pinia */ export { setActivePinia, getActivePinia } from './rootStore' -export { createPinia } from './createPinia' +export { createPinia, disposePinia } from './createPinia' export type { Pinia, // TODO: remove in next release