Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,747 changes: 1,223 additions & 524 deletions examples/d1-worker/package-lock.json

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions examples/d1-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
"test:run": "vitest --run"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.21.3",
"@cloudflare/vitest-plugin": "^1.4.7",
"@types/node": "^24.13.3",
"typescript": "^5.9.3",
"vitest": "^4.1.10",
"wrangler": "^4.123.0"
"vitest": "^4.1.12",
"wrangler": "^4.152.0"
},
"dependencies": {
"hono": "^4.13.2"
"hono": "^4.13.14"
},
"overrides": {
"undici": "^7.30.1",
"zod": "^4.6.6"
}
}
2 changes: 1 addition & 1 deletion examples/d1-worker/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": { "types": ["@cloudflare/vitest-pool-workers/types"] },
"compilerOptions": { "types": ["@cloudflare/vitest-plugin/types"] },
"include": ["./**/*.ts", "../worker-configuration.d.ts"],
"exclude": []
}
2 changes: 1 addition & 1 deletion examples/d1-worker/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
import { unstable_splitSqlQuery } from "wrangler";

Expand Down
2,178 changes: 1,815 additions & 363 deletions examples/d1-worker/worker-configuration.d.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion skills/sqlc-d1-typescript/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ generated file.
Generation and compilation say nothing about execution. One test, against local D1, executes a generated query
descriptor through the query executor and asserts the mapped row it returns.

When the project has no `@cloudflare/vitest-pool-workers` harness, `reference/local-d1-test.md` carries the
When the project has no `@cloudflare/vitest-plugin` harness, `reference/local-d1-test.md` carries the
wiring: dev dependencies, the binding, the config, per-test schema application, and the spec.

**Done when** one test executes a generated query descriptor against local D1, asserts the mapped row, and
Expand Down
38 changes: 20 additions & 18 deletions skills/sqlc-d1-typescript/reference/local-d1-test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Wiring a local D1 test

Reached from step 6 of `SKILL.md` when the project has no `@cloudflare/vitest-pool-workers` harness. A project
Reached from step 6 of `SKILL.md` when the project has no `@cloudflare/vitest-plugin` harness. A project
that already runs Worker tests keeps its own harness and only adds the one spec at the end of this page.

Everything here runs locally against Miniflare-backed D1 with isolated storage per test. No Cloudflare account,
Expand All @@ -24,7 +24,7 @@ Substitute the project's own schema, query, and generated factory throughout.
## 1. Dev dependencies

```sh
npm install --save-dev @cloudflare/vitest-pool-workers vitest wrangler
npm install --save-dev @cloudflare/vitest-plugin vitest wrangler
```

The exact versions this Plugin version was verified against are in
Expand Down Expand Up @@ -55,29 +55,30 @@ Run `wrangler types` after editing this file so `Env` carries the binding.

## 3. `vitest.config.ts`

`defineWorkersConfig` reads the same `wrangler.jsonc`. The schema is split into statements at config time and
The `cloudflareTest` plugin reads the same `wrangler.jsonc`. The schema is split into statements at config time and
handed to the test environment as a binding, so each test can apply it to its own isolated storage.
`remoteBindings: false` keeps every run local.

```ts
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
import { unstable_splitSqlQuery } from "wrangler";

const migration = fileURLToPath(new URL("./migrations/0001_init.sql", import.meta.url));
const statements = unstable_splitSqlQuery(readFileSync(migration, "utf8"));

export default defineWorkersConfig({
export default defineConfig({
plugins: [
cloudflareTest({
wrangler: { configPath: "./wrangler.jsonc" },
remoteBindings: false,
miniflare: { bindings: { TEST_SCHEMA_QUERIES: JSON.stringify(statements) } },
}),
],
test: {
setupFiles: ["./test/setup.ts"],
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.jsonc" },
remoteBindings: false,
miniflare: { bindings: { TEST_SCHEMA_QUERIES: JSON.stringify(statements) } },
},
},
},
});
```
Expand All @@ -95,12 +96,12 @@ binding types:
}
```

Give the tests their own `test/tsconfig.json`, which extends it and adds the pool's types:
Give the tests their own `test/tsconfig.json`, which extends it and adds the plugin's types:

```jsonc
{
"extends": "../tsconfig.json",
"compilerOptions": { "types": ["@cloudflare/vitest-pool-workers"] },
"compilerOptions": { "types": ["@cloudflare/vitest-plugin/types"] },
"include": ["./**/*.ts", "../worker-configuration.d.ts"],
"exclude": [],
}
Expand All @@ -113,20 +114,21 @@ Step 5 of `SKILL.md` runs both: `tsc --noEmit` and `tsc -p test/tsconfig.json --
`test/env.d.ts` declares the extra binding:

```ts
declare module "cloudflare:test" {
interface ProvidedEnv extends Env {
declare namespace Cloudflare {
interface Env {
TEST_SCHEMA_QUERIES: string;
}
}
```

`test/setup.ts` applies the schema before each test, into storage that starts empty:
`test/setup.ts` clears every binding's storage with `reset()` and applies the schema before each test:

```ts
import { env } from "cloudflare:test";
import { env, reset } from "cloudflare:test";
import { beforeEach } from "vitest";

beforeEach(async () => {
await reset();
for (const statement of JSON.parse(env.TEST_SCHEMA_QUERIES) as string[]) await env.DB.prepare(statement).run();
});
```
Expand Down
Loading
Loading