Skip to content

[Bug] Vitest Test Suite Crashes with TypeError: localStorage.clear is not a function in Node.js 20+ / Bun. #1539

Open

Description

@Itzzavdheshh

Bug Description

Running the test suite fails immediately with TypeError: localStorage.clear is not a function. This happens because Node.js 20+ and Bun expose a native but incomplete globalThis.localStorage that lacks standard methods like .clear(). When jsdom also tries to set up its own simulated storage, the two implementations clash — and whichever partial native one wins causes test files that call localStorage.clear() to crash before any assertions even run.


Steps to Reproduce

  1. Clone the repository and install dependencies
  2. Ensure you are running Node.js 20+ or Bun as the JS runtime
  3. Run the test suite via bun run test or npx vitest
  4. Observe the crash — no tests execute

Expected Behavior

The test suite should run successfully. localStorage in the jsdom environment should be a fully functional mock with all standard Web Storage API methods (getItem, setItem, removeItem, clear, key, length) available.


Actual Behavior

The test runner crashes immediately with:

TypeError: localStorage.clear is not a function

This error surfaces in at least two test files:

  • src/components/__tests__/ThemeToggle.test.tsx (line 11)
  • src/lib/tests/editorPersistence.test.ts (line 17)

The root cause is in vitest.setup.ts, which does not guard against or replace the partial native localStorage that Node.js 20+ / Bun inject into globalThis before jsdom initializes.


Expected Behavior

The vitest.setup.ts file should detect when localStorage is missing .clear() (or is otherwise incomplete) and replace it with a full in-memory mock, e.g.:

if (typeof window !== 'undefined') {
  if (!window.localStorage || typeof window.localStorage.clear !== 'function') {
    let store: Record<string, string> = {};
    const mockLocalStorage = {
      getItem: (key: string) => store[key] || null,
      setItem: (key: string, value: string) => { store[key] = String(value); },
      removeItem: (key: string) => { delete store[key]; },
      clear: () => { store = {}; },
      length: 0,
      key: (index: number) => Object.keys(store)[index] || null,
    };
    Object.defineProperty(window, 'localStorage', { value: mockLocalStorage, writable: true });
    Object.defineProperty(globalThis, 'localStorage', { value: mockLocalStorage, writable: true });
  }
}

Browser and OS Info

  • OS: Windows 11
  • Browser: N/A (server-side / test runner environment)
  • Runtime: Node.js 20+ or Bun
  • Vitest version: (please fill in)

Video Format (if relevant)

N/A


Screenshots / Screen Recording

TypeError: localStorage.clear is not a function
  at vitest.setup.ts (or ThemeToggle.test.tsx:11 / editorPersistence.test.ts:17)

Activity

  1. added
    bug
    on Jun 6, 2026
  2. github-actions commented on Jun 6, 2026

    @github-actions
    Contributor

    👋 Thanks for opening an issue, @Itzzavdheshh!

    Our maintainers will review this shortly 🚀

    Before you start working

    • 📖 Read the CONTRIBUTING.md guide
    • 💬 Wait for a maintainer to assign you the issue before starting work
    • 🔍 Search for existing issues to avoid duplicates

    For GSSoC'26 contributors

    If you're contributing under GirlScript Summer of Code 2026, please mention it in your comment when requesting assignment:

    "I'd like to work on this — contributing under GSSoC'26"

    Architecture notes

    Reframe is a static Next.js app with output: "export". Key points:

    • All video processing uses FFmpeg.wasm (client-side, no server)
    • headers() in next.config.ts won't work — use vercel.json instead
    • not-found.tsx requires public/404.html + vercel.json routes config

    Happy contributing! ⭐ Star the repo if you find it useful!

  3. changed the title [-][Bug] **Vitest Test Suite Crashes with `TypeError: localStorage.clear is not a function` in Node.js 20+ / Bun**[/-] [+][Bug] Vitest Test Suite Crashes with `TypeError: localStorage.clear is not a function` in Node.js 20+ / Bun[/+] on Jun 6, 2026
  4. changed the title [-][Bug] Vitest Test Suite Crashes with `TypeError: localStorage.clear is not a function` in Node.js 20+ / Bun[/-] [+][Bug] Vitest Test Suite Crashes with `TypeError: localStorage.clear is not a function` in Node.js 20+ / Bun.[/+] on Jun 6, 2026
  5. Itzzavdheshh commented on Jun 6, 2026

    @Itzzavdheshh
    Author

    /assign

  6. github-actions commented on Jun 6, 2026

    @github-actions
    Contributor

    Hey @Itzzavdheshh, this issue has been assigned to you! 🎉 Please make progress within 5 days or it will be automatically unassigned due to inactivity.

  7. Itzzavdheshh commented on Jun 6, 2026

    @Itzzavdheshh
    Author

    Hi @magic-peach Thanku for letting me work on this issue .

  8. github-actions commented on Jun 8, 2026

    @github-actions
    Contributor

    Hey @Itzzavdheshh, just a friendly reminder that this issue has had no activity for 2 days. Please leave an update or let us know if you need help! ⏰

  9. github-actions commented on Jun 12, 2026

    @github-actions
    Contributor

    Hey @Itzzavdheshh, final warning! ⚠️ This issue has had no activity for 4 days and will be automatically unassigned in 24 hours if there is no update.

  10. Itzzavdheshh commented on Jun 12, 2026

    @Itzzavdheshh
    Author

    Hey @magic-peach i already raise a PR so i request you to please check the PR

  11. Itzzavdheshh commented on Jun 15, 2026

    @Itzzavdheshh
    Author

    Hey @magic-peach you there ?

  12. github-actions commented on Jun 21, 2026

    @github-actions
    Contributor

    This issue has been unassigned from @Itzzavdheshh due to 5 days of inactivity and is open for anyone to pick up — comment /assign to claim it!

  13. vipul674 commented on Jun 21, 2026

    @vipul674

    I would like to work on this under GSSoC'26. Please assign it to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtype:bugtype:testing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions