From 39eee6a48b5d84f916e90b513024e26e9c6d72c5 Mon Sep 17 00:00:00 2001 From: Shayne O'Sullivan Date: Wed, 6 Oct 2021 09:30:14 -0500 Subject: [PATCH] feat(warn): log store id with class constructor warning (#702) --- packages/pinia/__tests__/store.spec.ts | 9 +++++---- packages/pinia/src/store.ts | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) 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}".` ) } -- 2.47.3