From bb8bf60581747c76e42796b82eb3aa04e2336fdf Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 17 Oct 2023 10:04:46 +0200 Subject: [PATCH] feat: disposePinia Close vuejs/pinia#2453 --- packages/pinia/src/createPinia.ts | 15 +++++++++++++++ packages/pinia/src/index.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) 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