|
| 1 | +import dedent from "ts-dedent"; |
| 2 | +import { test } from "./helpers"; |
| 3 | + |
| 4 | +test("hello_world support", async ({ expect, seed, vitestRun }) => { |
| 5 | + await seed({ |
| 6 | + "vitest.config.mts": dedent` |
| 7 | + import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; |
| 8 | + export default defineWorkersConfig({ |
| 9 | + test: { |
| 10 | + poolOptions: { |
| 11 | + workers: { |
| 12 | + singleWorker: true, |
| 13 | + wrangler: { configPath: "./wrangler.jsonc" }, |
| 14 | + }, |
| 15 | + }, |
| 16 | + } |
| 17 | + }); |
| 18 | + `, |
| 19 | + "wrangler.jsonc": dedent` |
| 20 | + { |
| 21 | + "compatibility_date": "2025-01-01", |
| 22 | + "unsafe_hello_world": [ |
| 23 | + { |
| 24 | + "binding": "HELLO_WORLD", |
| 25 | + } |
| 26 | + ] |
| 27 | + } |
| 28 | + `, |
| 29 | + "index.ts": dedent` |
| 30 | + export default { |
| 31 | + async fetch(request, env, ctx) { |
| 32 | + const value = Math.floor(Date.now() * Math.random()).toString(36); |
| 33 | + await env.HELLO_WORLD.set(value); |
| 34 | +
|
| 35 | + const result = await env.HELLO_WORLD.get(); |
| 36 | + if (value !== result.value) { |
| 37 | + return new Response("Value mismatch", { status: 500 }); |
| 38 | + } |
| 39 | +
|
| 40 | + return new Response('ok'); |
| 41 | + } |
| 42 | + } |
| 43 | + `, |
| 44 | + "index.test.ts": dedent` |
| 45 | + import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test"; |
| 46 | + import { it, expect } from "vitest"; |
| 47 | + import worker from "./index"; |
| 48 | + it("works", async () => { |
| 49 | + const request = new Request("http://example.com"); |
| 50 | + const ctx = createExecutionContext(); |
| 51 | + const response = await worker.fetch(request, env, ctx); |
| 52 | + await waitOnExecutionContext(ctx); |
| 53 | + expect(await response.text()).toBe("ok"); |
| 54 | + }); |
| 55 | + `, |
| 56 | + }); |
| 57 | + |
| 58 | + const result = await vitestRun(); |
| 59 | + await expect(result.exitCode).resolves.toBe(0); |
| 60 | +}); |
0 commit comments