From ce6395550c3e8fe05b3a9289d0dcdc5ecc4ca7fe Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Tue, 23 Apr 2024 16:55:39 +0200 Subject: [PATCH] feat(store): Throw if store not initialized Signed-off-by: Roberto Bianchi --- asyncforge.js | 5 ++++- test/do-not-start-memo.test.js | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/asyncforge.js b/asyncforge.js index 2ac45a4..00881f4 100644 --- a/asyncforge.js +++ b/asyncforge.js @@ -36,7 +36,10 @@ function memo (name) { function get () { const store = asyncLocalStorage.getStore() - return store?.[sym] + if (!store) { + throw new Error('asyncforge store is not initialized') + } + return store[sym] } function set (value) { diff --git a/test/do-not-start-memo.test.js b/test/do-not-start-memo.test.js index 13e6c52..0bd6307 100644 --- a/test/do-not-start-memo.test.js +++ b/test/do-not-start-memo.test.js @@ -8,7 +8,7 @@ test('memo without start', async (t) => { const p = tspl(t, { plan: 6 }) const a = memo() - p.deepStrictEqual(a(), undefined) + p.throws(() => a()) a.set({ value: 'bar' }) p.deepStrictEqual(a(), { value: 'bar' }) @@ -42,7 +42,7 @@ test('nested', async (t) => { const p = tspl(t, { plan: 5 }) const a = memo() - p.deepStrictEqual(a(), undefined) + p.throws(() => a()) a.set({ value: 'bar' }) p.deepStrictEqual(a(), { value: 'bar' })