Skip to content

feat: warn when create store with exsist id #1564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/pinia/__tests__/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 create store with exsist 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();
});
});
6 changes: 6 additions & 0 deletions packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ export function defineStore(
options = idOrOptions
id = idOrOptions.id
}
// push id into stores array

function useStore(pinia?: Pinia | null, hot?: StoreGeneric): StoreGeneric {
const currentInstance = getCurrentInstance()
Expand Down Expand Up @@ -900,6 +901,11 @@ export function defineStore(
// @ts-expect-error: not the right inferred type
useStore._pinia = pinia
}
} else {
console.warn(
`Store with id: ${id} already exists. Stores should have unique id.`,
'warn'
)
}

const store: StoreGeneric = pinia._s.get(id)!
Expand Down