Skip to content

Hold already-loaded lazy modules per test - #7746

Merged
ymichael merged 1 commit into
mainfrom
bb/lazy-module-hold-loaded-and-plugins
Nov 18, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/lazy-module-hold-loaded-and-plugins

Conversation

@ymichael

@ymichael ymichael commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#7736 holds each dynamic import() in @bb/app tests until the test settles it, but only the call itself. Once a defineSplit split or a React.lazy component has loaded in a worker, later tests render it synchronously, so the hold never applies to them. A test that asserts lazy content without waiting therefore passes only when an earlier test or file loaded the module first.

ProjectPathDialog.test.tsx on main shows this. In default order it passes: test 1 awaits findByRole, which loads RemotePathBrowser, and test 2 then reads it synchronously. Run alone (-t "preserves the direct single-machine folder flow") it fails, and with --sequence.shuffle seeds 1, 3 and 8 fail (seed 1 is the nightly app-3 failure).

What changed

Per-test gate for already-loaded modules (apps/app/src/test/lazy-modules.ts, setup.ts). The app test setup now mocks two lazy helpers:

  • It wraps defineSplit's loaded view with gateLoadedComponent.
  • It replaces React's lazy with gateReactLazy.

No production code changes. In each test, a split or lazy component that loaded before that test renders its real loading state on its first render, until the test settles (settleLazyModules(), findBy or waitFor). A failing test names it, for example split:remote-path-browser (held).

Two cases render at once:

  • Loaded in the same test. A production preload that warms a module counts as legitimate.
  • Preloaded by the file during collection. A top-level await Split.preload() exempts that split for the file's tests. That is explicit, order-independent fixture preparation.

settleLazyModules() also loops until no new import starts during a pass. This covers an import started in a microtask after render, which could outlast findBy's deadline.

Tests moved onto the signal.

  • ProjectPathDialog: test 2 awaits the folder browser.
  • FilePreview: the .htm case settles before reading the source view.
  • ThreadDetailSecondaryContent: drops its four 5 s findBy timeouts and its beforeAll preload.
  • define-split.test.tsx: tests defineSplit's own Suspense and preload mechanics, so it unmocks react and ./define-split.

Docs: docs/ui-code-splitting.md ("Test lazy boundaries") describes the gate, the collection-time preload exemption and the unmock for mechanism tests.

Scope. An earlier revision moved the hold into @get-bb/plugin-sdk as six experimental_ exports for plugins and @bb/shared-ui. That move is dropped, so this PR has no SDK, plugin, shared-ui or version changes. In-repo plugins have no lazy UI that their tests exercise:

  • Composer has one import(), already covered by vi.dynamicImportSettled().
  • Monaco's tests mock its loader.

The hold can move into the SDK when a plugin needs it.

How you verified

  • Repro on main: ProjectPathDialog passes in default order, fails when the test runs alone, and fails with shuffle seeds 1, 3 and 8.
  • Fix on this branch: it passes alone, in default order and with seeds 1–8.
  • Gate catches it deterministically: main's unfixed ProjectPathDialog.test.tsx fails under this branch's gate in default order, with Lazy imports had not settled when this test failed: split:remote-path-browser (held).
  • Mechanism tests (apps/app/src/test/lazy-modules.test.tsx) cover:
    • an already-loaded split and React.lazy held until the test settles;
    • a same-test load rendering at once;
    • a collection-time preload rendering at once;
    • a microtask-after-render import.
  • Local checks: pnpm exec turbo run typecheck lint lint:type-aware test --filter=@bb/app passes (689 test files).

AGENT GENERATED

🤖 Generated with Claude Code

The app's lazy-import hold only held the import() call. A split or React.lazy
component loaded by an earlier test rendered synchronously in later tests, so a
test that asserted lazy content without waiting passed or failed with test
order (ProjectPathDialog's single-machine test failed under shuffled seeds).

The app test setup now gates defineSplit's loaded view and React.lazy so a
module loaded before the current test renders its loading state on its first
render until the test settles. A module loaded in the same test, or preloaded
by the file during collection, renders at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ymichael
ymichael force-pushed the bb/lazy-module-hold-loaded-and-plugins branch from e79922d to ce86bb3 Compare November 18, 2026 04:23
@ymichael ymichael changed the title Hold already-loaded lazy modules per test and share the hold with plugins Hold already-loaded lazy modules per test Nov 18, 2026
@ymichael
ymichael merged commit 39ad460 into main Nov 18, 2026
53 checks passed
@ymichael
ymichael deleted the bb/lazy-module-hold-loaded-and-plugins branch November 18, 2026 04:29
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.

1 participant