Skip to content

fix(astro): run the e2e dev server in the foreground - #814

Open
maoberlehner wants to merge 3 commits into
mainfrom
fix/DX-582-astro-test-flake
Open

maoberlehner wants to merge 3 commits into
mainfrom
fix/DX-582-astro-test-flake

Conversation

@maoberlehner

@maoberlehner maoberlehner commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

The Astro end-to-end suite has been failing locally while passing in CI. It is
not a flake in the usual sense: there are two separate defects, and the first
one hid the second.

The dev server was never actually running

astro dev daemonizes when it detects an agentic environment — it forks the
server, prints Dev server running at http://localhost:4321 (pid N), and the
foreground process exits 0. start-server-and-test treats its child exiting as
the server dying and aborts with server closed unexpectedly before Cypress
ever starts.

That is why the failure looked environmental: CI runs no agent, so there the
server stays in the foreground and the suite passes. Anyone driving the repo
from a coding agent gets a deterministic failure instead, which is how this got
recorded as a pre-existing failure on main in #807.

ASTRO_DEV_BACKGROUND suppresses the auto-detection, so the e2e target now
starts the playground through a playground:test:foreground script. The name
reads backwards on purpose: any non-empty value tells Astro the background
decision has already been made, so it skips detection and stays in the
foreground. docs/testing-patterns.md records this, because the next person to
hit it will be looking at server closed unexpectedly with no obvious link to
the dev server forking.

The plain playground:test script is untouched, so running the playground by
hand from an agent still gets the background server and astro dev logs.

The dev toolbar loses a race against dependency optimization

With the server actually up, Cypress ran and the first spec failed:

Failed to fetch dynamically imported module:
  http://localhost:4321/node_modules/.vite/deps/toolbar-<hash>.js?v=<hash>

The dev toolbar is pulled in through a dynamic import. When Vite re-optimizes
dependencies, the module that was already served still points at the previous
optimized file name, so the import rejects and Cypress fails the test on the
unhandled rejection. Clearing the dependency cache between runs does not settle
it — three consecutive clean-cache runs went pass, pass, fail — because it is a
race, not a stale-cache artifact.

In a browser this is invisible: Vite reloads the page. It is only fatal under
Cypress, which fails a test on any unhandled rejection from the application. So
this is a harness artifact rather than a product defect, and the fix is to take
the toolbar out of a suite that never tested it. The test playground gates it on
STORYBLOK_E2E, which only the e2e script sets, so running the playground by
hand still gets the toolbar. No spec covers it before or after this change.

Verification

Five consecutive test:e2e runs pass with all five specs, and leave no dev
server behind on ports 4321-4325. pnpm nx test @storyblok/astro passes twice
in a row with the cache skipped. Before this, the suite never got past starting
the server.

Fixes DX-582

`astro dev` daemonizes when it detects an agentic environment: it forks the
server, prints the pid and exits. `start-server-and-test` reads its child
exiting as the server dying and aborts with "server closed unexpectedly", so
the suite failed for anyone running it from a coding agent while passing in
CI. Setting `ASTRO_DEV_BACKGROUND` suppresses the auto-detection, which is what
the new `playground:test:foreground` script exists to do.

Reaching Cypress at all then exposed a second failure. The Astro dev toolbar is
loaded through a dynamic import, and when Vite re-optimizes dependencies the
already-served module still points at the previous optimized file name, so the
import rejects and Cypress fails the run on the unhandled rejection. It is a
race, so clearing the dependency cache between runs does not settle it. The
toolbar is dev-only and no spec covers it, so the test playground turns it off.

Five consecutive runs pass and leave no dev server behind; before this the
suite never got past starting the server.

Fixes DX-582
Gating the toolbar on `STORYBLOK_E2E` rather than switching it off outright, so
running the test playground by hand still gets it.
@pkg-pr-new

pkg-pr-new Bot commented Sep 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

@storyblok/angular

npm i https://pkg.pr.new/@storyblok/angular@814

@storyblok/astro

npm i https://pkg.pr.new/@storyblok/astro@814

@storyblok/api-client

npm i https://pkg.pr.new/@storyblok/api-client@814

storyblok

npm i https://pkg.pr.new/storyblok@814

@storyblok/experiments

npm i https://pkg.pr.new/@storyblok/experiments@814

@storyblok/js

npm i https://pkg.pr.new/@storyblok/js@814

storyblok-js-client

npm i https://pkg.pr.new/storyblok-js-client@814

@storyblok/lint-config

npm i https://pkg.pr.new/@storyblok/lint-config@814

@storyblok/live-preview

npm i https://pkg.pr.new/@storyblok/live-preview@814

@storyblok/management-api-client

npm i https://pkg.pr.new/@storyblok/management-api-client@814

@storyblok/migrations

npm i https://pkg.pr.new/@storyblok/migrations@814

@storyblok/nuxt

npm i https://pkg.pr.new/@storyblok/nuxt@814

@storyblok/react

npm i https://pkg.pr.new/@storyblok/react@814

@storyblok/region-helper

npm i https://pkg.pr.new/@storyblok/region-helper@814

@storyblok/richtext

npm i https://pkg.pr.new/@storyblok/richtext@814

@storyblok/schema

npm i https://pkg.pr.new/@storyblok/schema@814

@storyblok/svelte

npm i https://pkg.pr.new/@storyblok/svelte@814

@storyblok/vue

npm i https://pkg.pr.new/@storyblok/vue@814

commit: 548dd12

@dipankarmaikap dipankarmaikap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

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