From: Shayne O'Sullivan Date: Wed, 6 Oct 2021 14:30:14 +0000 (-0500) Subject: feat(warn): log store id with class constructor warning (#702) X-Git-Tag: pinia@2.0.0-rc.12~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39eee6a48b5d84f916e90b513024e26e9c6d72c5;p=thirdparty%2Fvuejs%2Fpinia.git feat(warn): log store id with class constructor warning (#702) --- diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index 1bae5068..c7df724d 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -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) }) }) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index ca0b69d4..0a31d46c 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -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}".` ) }