Repository navigation
[vitest-plugin] Provide a way to start each test file's storage from a pre-seeded state (avoid re-applying D1 migrations per file) #16895
workers-devprod
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Moved from #16296. Originally opened by vanbroup on 2026-10-15.
What versions & operating system are you using?
@cloudflare/vitest-plugin1.4.3,vitest5.x,miniflare5.20261012.0-beta,workerd1.20261012.1Describe the problem
Each test file gets a fresh Miniflare (
CloudflarePoolWorker.start()creates one andstop()disposes it). That gives good isolation, but any storage a suite needs, most commonly a D1 schema from migrations, must be rebuilt from scratch in every file. The documented pattern isapplyD1Migrations()in asetupFilesentry, so the cost is paid once per file.In a suite with a few hundred test files and a typical migration history, applying about 40 migrations (about 1,500 statements) takes 0.7 to 1.4 s per file, about 20% of total wall time. Batching does not help: the time is SQLite executing the statements, not round trips. The work is identical in every file and produces the same sqlite file each time.
Minimal reproduction
DBand, say, 40 migration files (a few CREATE TABLE and CREATE INDEX statements each).vitest.config.ts:cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" }, miniflare: { bindings: { TEST_MIGRATIONS: await readD1Migrations("./migrations") } } })plussetupFiles: ["./test/apply-migrations.ts"].test/apply-migrations.ts:await applyD1Migrations(env.DB, env.TEST_MIGRATIONS).it("works", async () => { await env.DB.prepare("SELECT 1").first(); }).vitest runwith the setupFile versus without it: the difference grows linearly with file count (about N x migration time).Why existing options do not help
resourcePersistencePath, and the D1 data is already written to files under<root>/d1. But the plugin builds the Miniflare options itself (convertV4MiniflareOptions({ log, verbose, inspectorPort, unsafeModuleFallbackService, workers })), and the user-facingminiflareoption is worker-level only, so shared options cannot be set.Proposed solution (open to alternatives)
Let a project describe storage to seed once, and have each test file's Miniflare start from a private copy of it:
Properties: every file still gets its own writable copy (isolation unchanged), copies are removed on
stop(), the template is read-only after creation, Windows paths keep using the existing forward-slash normalization, and no Miniflare change is needed sinceresourcePersistencePathis already supported there. Copy-on-write (COPYFILE_FICLONE) keeps per-file cost near zero where the filesystem supports it.Would the team accept this direction? If so, which shape do you prefer (A directory, B declarative, or a Miniflare-level snapshot primitive)? I am happy to implement it if a maintainer is willing to vouch for the PR per CONTRIBUTING.md.
Additional notes
The README still lists "isolated per-test storage"; the current model is per-file isolation via a fresh Miniflare, which may be worth aligning while this is discussed.
workers-devprod commented on 2026-10-15:
Automated triage
Issue assessment
package:vitest(high)enhancement(high)product:d1(medium)Actionability and missing information
Well-documented feature request for per-file pre-seeded storage in the vitest plugin: fresh Miniflare per test file forces re-applying D1 migrations each time (~0.7–1.4 s/file, ~20% wall time). Includes versions, minimal repro, why existing options fall short, and proposed API shapes.
Reproduction reason: The report gives concrete benign steps, versions, and timing figures for the current per-file D1 migration cost in the vitest plugin, so the observed behavior can be verified.
Reproduction
Evidence and attempted steps
Reason: The issue is a feature request, not a behavior defect: it proposes a new storageSeed option (directory-copy or declarative D1-migration seeding) for the cloudflare vitest plugin and asks maintainers whether they would accept the direction and which design shape they prefer. The observed per-file migration cost follows the plugin's documented design (fresh Miniflare per test file with applyD1Migrations in setupFiles), so no reproduction could confirm or refute a defect.
This summary was generated by an LLM and may contain errors. Please verify its findings.
Triage bot commands and options
Repository collaborators with write or admin access can trigger actions by commenting on this issue:
@workers-devprod reproduce: Attempt to reproduce the issue using its description and recent comments.kanchingyg commented on 2026-10-25:
If you can run unit/integration tests against a local SQLite-backed
D1Databaseshim instead of spinning upapplyD1Migrations()inside Miniflare per file, I builtcreateD1SnapshotSeeder(wrangler-doctor/test) for this exact bottleneck. It applies yourmigrations/directory once and clones fresh isolatedD1Databaseinstances in ~0.3ms via in-memory SQLite serialization:It supports
prepare().bind().all()/first()/run()/raw(),batch(), andexec(), so existing Worker handlers acceptingenv.DBrun unchanged while waiting on nativeworkerdsnapshot hydration in@cloudflare/vitest-plugin.All reactions