From d13bffc99315da944eceac520dc72ee828110c83 Mon Sep 17 00:00:00 2001 From: Jay Meistrich Date: Sun, 26 Jan 2025 16:34:01 +0800 Subject: [PATCH] test: add a console warning when persistence is set up but without a name --- src/sync/syncObservable.ts | 12 ++++++++++++ tests/computed-persist.test.ts | 3 +++ tests/persist-indexeddb.test.ts | 17 ++++++++++++++++- tests/testglobals.ts | 7 +++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/sync/syncObservable.ts b/src/sync/syncObservable.ts index 87f6df87..ccf3aadf 100644 --- a/src/sync/syncObservable.ts +++ b/src/sync/syncObservable.ts @@ -913,6 +913,18 @@ async function loadLocal( ].filter(Boolean), ) as unknown as Promise; } else { + // Console.warn if no persist name is provided. It's ok if name is undefined as that can be the user manually disabling it, + // but if not provided at all it's likely user error. + if ( + (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') && + persist && + persist.plugin && + !Object.hasOwn(persist, 'name') + ) { + console.warn( + '[legend-state] Trying to syncObservable without `name` defined. Please include a `name` property in the `persist` configuration.', + ); + } nodeValue.resetPersistence = () => prevResetPersistence?.(); } // TODOV3 Remove diff --git a/tests/computed-persist.test.ts b/tests/computed-persist.test.ts index 8f9bb8ef..6dc2719f 100644 --- a/tests/computed-persist.test.ts +++ b/tests/computed-persist.test.ts @@ -83,6 +83,9 @@ describe('caching with new computed', () => { const nodes = observable( mySynced({ initial: [], + persist: { + name: getPersistName(), + }, }), ); diff --git a/tests/persist-indexeddb.test.ts b/tests/persist-indexeddb.test.ts index 901a2985..0e0e0fe8 100644 --- a/tests/persist-indexeddb.test.ts +++ b/tests/persist-indexeddb.test.ts @@ -6,7 +6,7 @@ import { configureSynced } from '../src/sync/configureSynced'; import { mapSyncPlugins, syncObservable } from '../src/sync/syncObservable'; import type { ObservablePersistPlugin, ObservablePersistPluginOptions } from '../src/sync/syncTypes'; import { when } from '../src/when'; -import { promiseTimeout } from './testglobals'; +import { expectLog, promiseTimeout } from './testglobals'; const TableNameBase = 'jestlocal'; const tableNames = Array.from({ length: 100 }, (_, i) => TableNameBase + i); @@ -371,4 +371,19 @@ describe('Persist IDB', () => { await expectIDB(persistName, []); }); + test('Persist without a name errors', async () => { + const obs = observable>({}); + + expectLog( + () => { + syncObservable(obs, { + persist: { + plugin: myIndexedDBPlugin, + }, + }); + }, + '[legend-state] Trying to syncObservable without `name` defined. Please include a `name` property in the `persist` configuration.', + 'warn', + ); + }); }); diff --git a/tests/testglobals.ts b/tests/testglobals.ts index 74c9deda..d16b748f 100644 --- a/tests/testglobals.ts +++ b/tests/testglobals.ts @@ -83,3 +83,10 @@ export function supressActWarning(fn: () => void) { console.error = originalError; } + +export function expectLog(fn: () => any, msg: string, logType: 'log' | 'warn' | 'error') { + jest.spyOn(console, logType).mockImplementation(() => {}); + fn(); + expect(console[logType]).toHaveBeenCalledWith(msg); + (console[logType] as jest.Mock).mockRestore(); +}