diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index 4e0a21ad7e..55959c6133 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -379,4 +379,12 @@ 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('warns when creating store with existing id', async () => { + const id = 'test id'; + const useFirstStore = defineStore(id, {}); + const useSecondStore = defineStore(id, {}); + useFirstStore(); + useSecondStore(); + expect(`Store with id: ${id} already exists. Stores should have unique id.`).toHaveBeenWarned(); + }); +}); diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index d7156a0ea6..3562736b7e 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -142,7 +142,7 @@ function createOptionsStore< const localState = __DEV__ && hot ? // use ref() to unwrap refs inside state TODO: check if this is still necessary - toRefs(ref(state ? state() : {}).value) + toRefs(ref(state ? state() : {}).value) : toRefs(pinia.state.value[id]) return assign( @@ -316,10 +316,10 @@ function createSetupStore< /* istanbul ignore next */ const $reset = __DEV__ ? () => { - throw new Error( - `🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().` - ) - } + throw new Error( + `🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().` + ) + } : noop function $dispose() { @@ -441,10 +441,10 @@ function createSetupStore< assign( __DEV__ && IS_CLIENT ? // devtools custom properties - { - _customProperties: markRaw(new Set()), - _hmrPayload, - } + { + _customProperties: markRaw(new Set()), + _hmrPayload, + } : {}, partialStore // must be added later @@ -522,7 +522,7 @@ function createSetupStore< if (isComputed(prop)) { _hmrPayload.getters[key] = isOptionsStore ? // @ts-expect-error - options.getters[key] + options.getters[key] : prop if (IS_CLIENT) { const getters: string[] = @@ -620,10 +620,10 @@ function createSetupStore< const getter: _Method = newStore._hmrPayload.getters[getterName] const getterValue = isOptionsStore ? // special handling of options api - computed(() => { - setActivePinia(pinia) - return getter.call(store, store) - }) + computed(() => { + setActivePinia(pinia) + return getter.call(store, store) + }) : getter set(store, getterName, getterValue) @@ -715,8 +715,8 @@ function createSetupStore< ) { console.warn( `[🍍]: The "state" must be a plain object. It cannot be\n` + - `\tstate: () => new MyClass()\n` + - `Found in store "${store.$id}".` + `\tstate: () => new MyClass()\n` + + `Found in store "${store.$id}".` ) } @@ -845,17 +845,17 @@ export function defineStore( let id: string let options: | DefineStoreOptions< - string, - StateTree, - _GettersTree, - _ActionsTree - > + string, + StateTree, + _GettersTree, + _ActionsTree + > | DefineSetupStoreOptions< - string, - StateTree, - _GettersTree, - _ActionsTree - > + string, + StateTree, + _GettersTree, + _ActionsTree + > const isSetupStore = typeof setup === 'function' if (typeof idOrOptions === 'string') { @@ -879,9 +879,9 @@ export function defineStore( if (__DEV__ && !activePinia) { throw new Error( `[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n` + - `\tconst pinia = createPinia()\n` + - `\tapp.use(pinia)\n` + - `This will fail in production.` + `\tconst pinia = createPinia()\n` + + `\tapp.use(pinia)\n` + + `This will fail in production.` ) } @@ -900,6 +900,10 @@ export function defineStore( // @ts-expect-error: not the right inferred type useStore._pinia = pinia } + } else { + if (__DEV__) console.warn( + `Store with id: ${id} already exists. Stores should have unique id.` + ) } const store: StoreGeneric = pinia._s.get(id)!