]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(warn): log store id with class constructor warning (#702)
authorShayne O'Sullivan <shayneosull@gmail.com>
Wed, 6 Oct 2021 14:30:14 +0000 (09:30 -0500)
committerGitHub <noreply@github.com>
Wed, 6 Oct 2021 14:30:14 +0000 (16:30 +0200)
packages/pinia/__tests__/store.spec.ts
packages/pinia/src/store.ts

index 1bae5068dbbf84635ef6163eaf64f8c6c70b59b0..c7df724dfd6647eb60e67e3c932e5cac66b165bc 100644 (file)
@@ -334,7 +334,8 @@ describe('Store', () => {
     expect(useStore()).not.toBe(store)
   })
 
-  const warnTextCheckPlainObject = `"state" must be a plain object`
+  const warnTextCheckPlainObject = (storeId: string) =>
+    `The "state" must be a plain object. It cannot be\n\tstate: () => new MyClass()\nFound in store "${storeId}".`
 
   it('warns when state is created with a class constructor', () => {
     class MyState {}
@@ -344,7 +345,7 @@ describe('Store', () => {
       state: () => new MyState(),
     })
     useMyStore()
-    expect(warnTextCheckPlainObject).toHaveBeenWarned()
+    expect(warnTextCheckPlainObject('store')).toHaveBeenWarned()
   })
 
   it('only warns about constructors when store is initially created', () => {
@@ -354,7 +355,7 @@ describe('Store', () => {
       state: () => new MyState(),
     })
     useMyStore()
-    expect(warnTextCheckPlainObject).toHaveBeenWarnedTimes(1)
+    expect(warnTextCheckPlainObject('arrowInit')).toHaveBeenWarnedTimes(1)
   })
 
   it('does not warn when state is created with a plain object', () => {
@@ -363,6 +364,6 @@ describe('Store', () => {
       state: () => ({ someValue: undefined }),
     })
     useMyStore()
-    expect(warnTextCheckPlainObject).toHaveBeenWarnedTimes(0)
+    expect(warnTextCheckPlainObject('poInit')).toHaveBeenWarnedTimes(0)
   })
 })
index ca0b69d4b90480b90b5726ddc8613e81af69a9b2..0a31d46c39f7363bd6012f9af642b0e10345b2e4 100644 (file)
@@ -676,7 +676,7 @@ function createSetupStore<
     !store.$state.constructor.toString().includes('[native code]')
   ) {
     console.warn(
-      `[🍍]: The "state" must be a plain object. It cannot be\n\tstate: () => new MyClass()`
+      `[🍍]: The "state" must be a plain object. It cannot be\n\tstate: () => new MyClass()\nFound in store "${store.$id}".`
     )
   }