|
1 | 1 | import dedent from "ts-dedent"; |
2 | 2 | import { minimalVitestConfig, test, waitFor } from "./helpers"; |
3 | 3 |
|
| 4 | +const durableObjectWorker = dedent` |
| 5 | + export class Counter { |
| 6 | + count = 0; |
| 7 | +
|
| 8 | + constructor(readonly state: DurableObjectState) { |
| 9 | + void state.blockConcurrencyWhile(async () => { |
| 10 | + this.count = (await state.storage.get("count")) ?? 0; |
| 11 | + }); |
| 12 | + } |
| 13 | +
|
| 14 | + fetch(request: Request) { |
| 15 | + this.count++; |
| 16 | + void this.state.storage.put("count", this.count); |
| 17 | + return new Response(this.count.toString()); |
| 18 | + } |
| 19 | + } |
| 20 | +
|
| 21 | + export default { |
| 22 | + fetch(request: Request, env: any) { |
| 23 | + const { pathname } = new URL(request.url); |
| 24 | + const id = env.COUNTER.idFromName(pathname); |
| 25 | + const stub = env.COUNTER.get(id); |
| 26 | + return stub.fetch(request); |
| 27 | + } |
| 28 | + } |
| 29 | +`; |
| 30 | + |
| 31 | +function makeDurableObjectConfig(isolatedStorage: boolean) { |
| 32 | + return dedent` |
| 33 | + import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; |
| 34 | + export default defineWorkersConfig({ |
| 35 | + test: { |
| 36 | + poolOptions: { |
| 37 | + workers: { |
| 38 | + main: "./index.ts", |
| 39 | + singleWorker: true, |
| 40 | + isolatedStorage: ${isolatedStorage}, |
| 41 | + miniflare: { |
| 42 | + compatibilityDate: "2024-01-01", |
| 43 | + compatibilityFlags: ["nodejs_compat"], |
| 44 | + durableObjects: { |
| 45 | + COUNTER: "Counter", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + }); |
| 52 | + `; |
| 53 | +} |
| 54 | + |
| 55 | +// when isolatedStorage: true storage be reset at every test |
| 56 | +const isolatedStorageTests = dedent` |
| 57 | + import { SELF } from "cloudflare:test"; |
| 58 | + import { it, expect } from "vitest"; |
| 59 | +
|
| 60 | + it("first test: verifies incrementing works", async () => { |
| 61 | + // First fetch returns 1 |
| 62 | + let response = await SELF.fetch("https://example.com/test-path"); |
| 63 | + expect(await response.text()).toBe("1"); |
| 64 | + // Second fetch returns 2 (counter increments) |
| 65 | + response = await SELF.fetch("https://example.com/test-path"); |
| 66 | + expect(await response.text()).toBe("2"); |
| 67 | + // Third fetch returns 3 |
| 68 | + response = await SELF.fetch("https://example.com/test-path"); |
| 69 | + expect(await response.text()).toBe("3"); |
| 70 | + }); |
| 71 | +
|
| 72 | + it("second test: storage is reset, starts at 1 again", async () => { |
| 73 | + // With isolatedStorage: true, storage is reset between tests |
| 74 | + const response = await SELF.fetch("https://example.com/test-path"); |
| 75 | + expect(await response.text()).toBe("1"); |
| 76 | + }); |
| 77 | +
|
| 78 | + it("third test: storage is reset, starts at 1 again", async () => { |
| 79 | + // With isolatedStorage: true, storage is reset between tests |
| 80 | + const response = await SELF.fetch("https://example.com/test-path"); |
| 81 | + expect(await response.text()).toBe("1"); |
| 82 | + }); |
| 83 | +`; |
| 84 | + |
| 85 | +// when isolatedStorage: false storage should leak in between tests |
| 86 | +const sharedStorageTests = dedent` |
| 87 | + import { SELF } from "cloudflare:test"; |
| 88 | + import { it, expect } from "vitest"; |
| 89 | +
|
| 90 | + it("first increment", async () => { |
| 91 | + const response = await SELF.fetch("https://example.com/test-path"); |
| 92 | + // First test in a run should always see "1" (storage reset between runs) |
| 93 | + expect(await response.text()).toBe("1"); |
| 94 | + }); |
| 95 | +
|
| 96 | + it("second increment", async () => { |
| 97 | + const response = await SELF.fetch("https://example.com/test-path"); |
| 98 | + // With isolatedStorage: false, storage leaks between tests |
| 99 | + expect(await response.text()).toBe("2"); |
| 100 | + }); |
| 101 | +
|
| 102 | + it("third increment", async () => { |
| 103 | + const response = await SELF.fetch("https://example.com/test-path"); |
| 104 | + // With isolatedStorage: false, storage leaks between tests |
| 105 | + expect(await response.text()).toBe("3"); |
| 106 | + }); |
| 107 | +`; |
| 108 | + |
| 109 | +test( |
| 110 | + "DO storage is reset between vitest runs (isolatedStorage: true)", |
| 111 | + { timeout: 60_000 }, |
| 112 | + async ({ expect, seed, vitestRun }) => { |
| 113 | + await seed({ |
| 114 | + "vitest.config.mts": makeDurableObjectConfig(true), |
| 115 | + "index.ts": durableObjectWorker, |
| 116 | + "index.test.ts": isolatedStorageTests, |
| 117 | + }); |
| 118 | + |
| 119 | + let result = await vitestRun(); |
| 120 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 121 | + expect(await result.exitCode).toBe(0); |
| 122 | + |
| 123 | + result = await vitestRun(); |
| 124 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 125 | + expect(await result.exitCode).toBe(0); |
| 126 | + } |
| 127 | +); |
| 128 | + |
| 129 | +test( |
| 130 | + "DO storage is reset between vitest runs (isolatedStorage: false)", |
| 131 | + { timeout: 60_000 }, |
| 132 | + async ({ expect, seed, vitestRun }) => { |
| 133 | + await seed({ |
| 134 | + "vitest.config.mts": makeDurableObjectConfig(false), |
| 135 | + "index.ts": durableObjectWorker, |
| 136 | + "index.test.ts": sharedStorageTests, |
| 137 | + }); |
| 138 | + |
| 139 | + let result = await vitestRun(); |
| 140 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 141 | + expect(await result.exitCode).toBe(0); |
| 142 | + |
| 143 | + result = await vitestRun(); |
| 144 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 145 | + expect(await result.exitCode).toBe(0); |
| 146 | + } |
| 147 | +); |
| 148 | + |
| 149 | +test( |
| 150 | + "DO storage is reset in watch mode (isolatedStorage: true)", |
| 151 | + { timeout: 50000 }, |
| 152 | + async ({ expect, seed, vitestDev }) => { |
| 153 | + await seed({ |
| 154 | + "vitest.config.mts": makeDurableObjectConfig(true), |
| 155 | + "index.ts": durableObjectWorker, |
| 156 | + "index.test.ts": isolatedStorageTests, |
| 157 | + }); |
| 158 | + |
| 159 | + const result = vitestDev(); |
| 160 | + |
| 161 | + await waitFor(() => { |
| 162 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 163 | + }); |
| 164 | + |
| 165 | + await seed({ |
| 166 | + "index.test.ts": isolatedStorageTests + "\n// trigger re-run", |
| 167 | + }); |
| 168 | + |
| 169 | + await waitFor(() => { |
| 170 | + const matches = result.stdout.match(/Tests\s+3 passed/g); |
| 171 | + expect(matches?.length).toBeGreaterThanOrEqual(2); |
| 172 | + }); |
| 173 | + } |
| 174 | +); |
| 175 | + |
| 176 | +test( |
| 177 | + "DO storage is reset in watch mode (isolatedStorage: false)", |
| 178 | + { timeout: 50000 }, |
| 179 | + async ({ expect, seed, vitestDev }) => { |
| 180 | + await seed({ |
| 181 | + "vitest.config.mts": makeDurableObjectConfig(false), |
| 182 | + "index.ts": durableObjectWorker, |
| 183 | + "index.test.ts": sharedStorageTests, |
| 184 | + }); |
| 185 | + |
| 186 | + const result = vitestDev(); |
| 187 | + |
| 188 | + await waitFor(() => { |
| 189 | + expect(result.stdout).toMatch(/Tests.*3 passed/s); |
| 190 | + }); |
| 191 | + |
| 192 | + await seed({ |
| 193 | + "index.test.ts": sharedStorageTests + "\n// trigger re-run", |
| 194 | + }); |
| 195 | + |
| 196 | + await waitFor(() => { |
| 197 | + const matches = result.stdout.match(/Tests\s+3 passed/g); |
| 198 | + expect(matches?.length).toBeGreaterThanOrEqual(2); |
| 199 | + }); |
| 200 | + } |
| 201 | +); |
| 202 | + |
4 | 203 | test("automatically re-runs unit tests", async ({ |
5 | 204 | expect, |
6 | 205 | seed, |
|
0 commit comments