Skip to content

Fix flaky embed.test.ts: identity loading outlives jsdom teardown #5

Description

@lsegal

app/src/embed.test.ts intermittently fails CI with an unhandled rejection after the jsdom environment is torn down:

ReferenceError: localStorage is not defined
 ❯ migrateLegacyDevice src/lib/identity.ts:204:18
 ❯ loadStoredDevice src/lib/identity.ts:227:26
 ❯ loadIdentity src/lib/identity.ts:452:27
 ❯ src/App.tsx:350:30
This error originated in "src/embed.test.ts" test file ... caught after test environment was torn down.

Example failing run: https://github.com/sssubscript/write/actions/runs/37154350731 (first attempt failed; the rerun passed with no code change). main has also had intermittent CI failures.

Cause

The embed tests mount App, which starts async identity loading (loadIdentity → IndexedDB → localStorage) in an effect. The tests only await a single setTimeout(0) tick, so that work can still be running when Vitest tears down jsdom, and the next localStorage access throws.

Acceptance criteria

  • The embed tests no longer leave identity loading running past teardown, for example by waiting for it to settle before unmounting or by stubbing loadIdentity in these tests.
  • Alternatively, or additionally, the identity loading effect in App ignores or cancels work after unmount.
  • pnpm run test passes reliably with no unhandled rejections, repeated (e.g. 20 consecutive runs of embed.test.ts).

Addresses #2 and #4

Activity

  1. self-assigned this
    on Oct 3, 2026
  2. lsegal commented on Oct 3, 2026

    @lsegal
    ContributorAuthor

    Starting work on this issue /glorp:45E62B60

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions