From a1510cd1310c5a28ce2f88ed8a9ef23eb10d9f93 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 4cc8895..c6ad798 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -30,3 +30,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 4b3a8c5..3301618 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