From da613dd71341a84180e04f258224475944e5d2e6 Mon Sep 17 00:00:00 2001 From: Rui Martins Date: Tue, 18 Oct 2022 14:59:22 +0100 Subject: [PATCH 1/2] chore: warn developers for duplicated store keys --- packages/pinia/__tests__/store.spec.ts | 14 +++++++++++++- packages/pinia/src/createPinia.ts | 1 + packages/pinia/src/rootStore.ts | 8 ++++++++ packages/pinia/src/store.ts | 8 ++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index 4e0a21ad7e..259b17e02c 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -101,7 +101,7 @@ describe('Store', () => { it('can create an empty state if no state option is provided', () => { const store = defineStore({ id: 'some' })() - + expect(store.$state).toEqual({}) }) @@ -379,4 +379,16 @@ describe('Store', () => { `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "anyName" in store "main".` ).toHaveBeenWarnedTimes(1) }) + + it.only('warns when creating store with existing id', async () => { + const storeId = 'testStoreID'; + const useFirstStore = defineStore(storeId, {}); + const useSecondStore = defineStore(storeId, {}); + useFirstStore(); + useSecondStore(); + + expect( + `[🍍]: Stores should have unique identifiers. Found multiple stores with id "testStoreID". Rename one of them.` + ).toHaveBeenWarned(); + }); }) diff --git a/packages/pinia/src/createPinia.ts b/packages/pinia/src/createPinia.ts index f71497fd31..90253bc3df 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -52,6 +52,7 @@ export function createPinia(): Pinia { _a: null, _e: scope, _s: new Map(), + _k: [], state, }) diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index 0509a5c8eb..9bbd3fa21e 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -85,6 +85,14 @@ export interface Pinia { */ _s: Map + /** + * Registry of store ids defined in this pinia instance. + * This is used to check for duplicated keys. + * + * @internal + */ + _k: Array + /** * Added by `createTestingPinia()` to bypass `useStore(pinia)`. * diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 98d77a2666..4504ee2127 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -880,6 +880,14 @@ export function defineStore( id = idOrOptions.id } + // Warn developers about existing store ID in dev environments + if (__DEV__ && activePinia?._k.includes(id)) { + console.warn(`[🍍]: Stores should have unique identifiers. Found multiple stores with id "${id}". Rename one of them.`) + } + + // Push ID to keys registry + activePinia?._k.push(id) + function useStore(pinia?: Pinia | null, hot?: StoreGeneric): StoreGeneric { const currentInstance = getCurrentInstance() pinia = From fbfc8304c4e62165a8bd5fd061fbb23a44a90863 Mon Sep 17 00:00:00 2001 From: Rui Martins Date: Tue, 18 Oct 2022 15:09:16 +0100 Subject: [PATCH 2/2] chore: remove "only" keyword on test --- packages/pinia/__tests__/store.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index 259b17e02c..cef8262448 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -101,7 +101,7 @@ describe('Store', () => { it('can create an empty state if no state option is provided', () => { const store = defineStore({ id: 'some' })() - + expect(store.$state).toEqual({}) }) @@ -380,7 +380,7 @@ describe('Store', () => { ).toHaveBeenWarnedTimes(1) }) - it.only('warns when creating store with existing id', async () => { + it('warns when creating store with existing id', async () => { const storeId = 'testStoreID'; const useFirstStore = defineStore(storeId, {}); const useSecondStore = defineStore(storeId, {});