Skip to content

Commit

Permalink
Expose sqlite in Vitest (#6725)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Howard <[email protected]>
  • Loading branch information
penalosa and joshthoward authored Sep 20, 2024
1 parent 9649dbc commit 92730bb
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-dolphins-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vitest-pool-workers": patch
---

fix: SQLite Durable Objects cannot be tested with vitest-pool-workers
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
interface Env {
COUNTER: DurableObjectNamespace;
SQL: DurableObjectNamespace;
}
12 changes: 12 additions & 0 deletions fixtures/vitest-pool-workers-examples/durable-objects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ export class Counter implements DurableObject {
}
}

export class SQLiteDurableObject implements DurableObject {
constructor(readonly ctx: DurableObjectState) {}
fetch() {
return new Response(this.ctx.storage.sql.databaseSize.toString());
}
}

export default <ExportedHandler<Env>>{
fetch(request, env) {
const { pathname } = new URL(request.url);
if (pathname === "/sql") {
const id = env.SQL.idFromName(pathname);
const stub = env.SQL.get(id);
return stub.fetch(request);
}
const id = env.COUNTER.idFromName(pathname);
const stub = env.COUNTER.get(id);
return stub.fetch(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SELF } from "cloudflare:test";
import { expect, it } from "vitest";

it("enables SQL API with migrations", async () => {
const response = await SELF.fetch("https://example.com/sql");
expect(await response.text()).toBe("4096");
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ compatibility_flags = ["nodejs_compat"]

[durable_objects]
bindings = [
{ name = "COUNTER", class_name = "Counter" }
{ name = "COUNTER", class_name = "Counter" },
{ name = "SQL", class_name = "SQLiteDurableObject" }
]

[[migrations]]
tag = "v1"
new_classes = ["Counter"]
new_sqlite_classes = ["SQLiteDurableObject"]
5 changes: 4 additions & 1 deletion fixtures/vitest-pool-workers-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"check:type": "node tsc-all.mjs",
"list": "vitest --config vitest.workers.config.ts list",
"test": "vitest --config vitest.workers.config.ts --reporter basic",
"test:ci": "vitest run --config vitest.workers.config.ts --reporter basic"
"test:ci": "run-script-os",
"test:ci:default": "vitest run --config vitest.workers.config.ts --reporter basic",
"test:ci:win32": "vitest run --config vitest.workers.config.ts --reporter basic --exclude test/sqlite-in-do.test.ts"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "workspace:*",
Expand All @@ -15,6 +17,7 @@
"ext-dep": "file:./internal-module-resolution/vendor/ext-dep",
"jose": "^5.2.2",
"miniflare": "workspace:*",
"run-script-os": "^1.1.6",
"toucan-js": "^3.3.1",
"typescript": "^5.5.2",
"vitest": "catalog:default",
Expand Down
26 changes: 18 additions & 8 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
buildMiniflareBindingOptions,
buildSitesOptions,
} from "../../../dev/miniflare";
import { getClassNamesWhichUseSQLite } from "../../../dev/validate-dev-props";
import { run } from "../../../experimental-flags";
import { getLegacyAssetPaths, getSiteAssetPaths } from "../../../sites";
import { CacheStorage } from "./caches";
Expand Down Expand Up @@ -284,15 +285,24 @@ export function unstable_getMiniflareWorkerOptions(
);
}
if (bindings.durable_objects !== undefined) {
type DurableObjectDefinition = NonNullable<
typeof bindingOptions.durableObjects
>[string];

const classNameToUseSQLite = getClassNamesWhichUseSQLite(config.migrations);

bindingOptions.durableObjects = Object.fromEntries(
bindings.durable_objects.bindings.map((binding) => [
binding.name,
{
className: binding.class_name,
scriptName: binding.script_name,
binding,
},
])
bindings.durable_objects.bindings.map((binding) => {
const useSQLite = classNameToUseSQLite.get(binding.class_name);
return [
binding.name,
{
className: binding.class_name,
scriptName: binding.script_name,
useSQLite,
} satisfies DurableObjectDefinition,
];
})
);
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92730bb

Please sign in to comment.