Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(vitest-pool-workers): Support specifying an environment in tests #5612

Merged
merged 1 commit into from
Apr 15, 2024
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
24 changes: 24 additions & 0 deletions .changeset/rich-jokes-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@cloudflare/vitest-pool-workers": minor
---

Feat: Support specifying an environment for your worker when running tests. This allows your tests to pick up bindings & variables that are scoped to specific environments.

For example:

```ts
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: {
configPath: "./wrangler.toml",
environment: "production",
},
},
},
},
});
```
1 change: 1 addition & 0 deletions fixtures/vitest-pool-workers-examples/d1/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineWorkersProject(async () => {
singleWorker: true,
wrangler: {
configPath: "./wrangler.toml",
environment: "production",
},
miniflare: {
// Add a test-only binding for migrations, so we can apply them in a
Expand Down
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/d1/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main = "src/index.ts"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_compat"]

[[ d1_databases ]]
[[env.production.d1_databases]]
CarmenPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
binding = "DATABASE"
database_name = "database"
database_id = "00000000-0000-0000-0000-000000000000"
9 changes: 7 additions & 2 deletions packages/vitest-pool-workers/src/pool/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const WorkersPoolOptionsSchema = z.object({
})
.passthrough()
.optional(),
wrangler: z.object({ configPath: z.ostring() }).optional(),
wrangler: z
.object({ configPath: z.ostring(), environment: z.ostring() })
.optional(),
});
export type SourcelessWorkerOptions = Omit<
WorkerOptions,
Expand Down Expand Up @@ -181,7 +183,10 @@ async function parseCustomPoolOptions(
// Lazily import `wrangler` if and when we need it
const wrangler = await import("wrangler");
const { workerOptions, define, main } =
wrangler.unstable_getMiniflareWorkerOptions(configPath);
wrangler.unstable_getMiniflareWorkerOptions(
configPath,
options.wrangler.environment
);

// If `main` wasn't explicitly configured, fall back to Wrangler config's
options.main ??= main;
Expand Down
10 changes: 8 additions & 2 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,18 @@ export type SourcelessWorkerOptions = Omit<
"script" | "scriptPath" | "modules" | "modulesRoot"
> & { modulesRules?: ModuleRule[] };

export function unstable_getMiniflareWorkerOptions(configPath: string): {
export function unstable_getMiniflareWorkerOptions(
configPath: string,
environment?: string
): {
workerOptions: SourcelessWorkerOptions;
define: Record<string, string>;
main?: string;
} {
const config = readConfig(configPath, { experimentalJsonConfig: true });
const config = readConfig(configPath, {
experimentalJsonConfig: true,
env: environment,
});

const modulesRules: ModuleRule[] = config.rules
.concat(DEFAULT_MODULE_RULES)
Expand Down
Loading