Skip to content

fix(tests): provide localStorage shim in jsdom test setup - #10

Merged
flowdeskadmin merged 1 commit into
Flowdesktech:masterfrom
dan-nyanko:fix/tests-localstorage-polyfill
Aug 25, 2026
Merged

fix(tests): provide localStorage shim in jsdom test setup#10
flowdeskadmin merged 1 commit into
Flowdesktech:masterfrom
dan-nyanko:fix/tests-localstorage-polyfill

Conversation

@dan-nyanko

Copy link
Copy Markdown
Contributor

Summary

projectsPersistence.test.ts and projectsSlice.test.ts were failing with TypeError: Cannot read properties of undefined (reading 'getItem') because localStorage was undefined in the test environment. This adds a minimal, guarded in-memory localStorage shim to src/test/setup.ts so tests pass consistently regardless of runtime.

Root cause

The repo's vitest config uses environment: 'jsdom', which normally exposes localStorage. However, on runtimes where jsdom does not reliably expose it (e.g. Node 26's experimental built-in localStorage shadows the jsdom one, emitting localStorage is not available because --localstorage-file was not provided), localStorage ends up undefined, breaking any test that reads/writes project state via localStorage.

Fix

In src/test/setup.ts, detect a missing localStorage and install an in-memory Storage shim on both globalThis and window. The shim is guarded by a typeof check, so it only applies when absent — behavior is unchanged on environments where jsdom already provides localStorage. The existing beforeEach cleanup is preserved.

Testing

  • pnpm run test → 61 passed (was 54 passed / 7 failed before the fix)
  • pnpm run lint, pnpm run format:check, pnpm run typecheck → all pass

Scope

Single-file change (src/test/setup.ts). No application/runtime code is affected; it only touches the test environment.

jsdom does not reliably expose localStorage across runtimes (e.g. Node 26's
experimental built-in shadows it), causing projectsPersistence/projectsSlice
tests to fail with 'localStorage is undefined'. Add a minimal in-memory shim
guarded by typeof check so it only applies when absent, keeping behavior
identical on environments where jsdom already provides it.

@flowdeskadmin flowdeskadmin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@flowdeskadmin
flowdeskadmin merged commit 834adb3 into Flowdesktech:master Aug 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants