The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | import { tmpdir } from "node:os"; |
| 3 | import { dirname, resolve } from "node:path"; |
| 4 | import { fileURLToPath, pathToFileURL } from "node:url"; |
| 5 | import { expect, it } from "@alchemy.run/test"; |
| 6 | |
| 7 | const here = dirname(fileURLToPath(import.meta.url)); |
| 8 | const cli = resolve(here, "../bin/alchemy-test.ts"); |
| 9 | const apiUrl = pathToFileURL(resolve(here, "../src/index.ts")).href; |
| 10 | const effectUrl = import.meta.resolve("effect/Effect"); |
| 11 | |
| 12 | it( |
| 13 | "excludes exclusive-lock queue time from test durations", |
| 14 | async () => { |
| 15 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-timing-")); |
| 16 | try { |
| 17 | await writeFile( |
| 18 | resolve(root, "timing.test.ts"), |
| 19 | ` |
| 20 | import { describe, it, expect } from ${JSON.stringify(apiUrl)}; |
| 21 | describe.concurrent("timing", () => { |
| 22 | let finished = false; |
| 23 | it("holder", async () => { |
| 24 | await new Promise(resolve => setTimeout(resolve, 500)); |
| 25 | finished = true; |
| 26 | }); |
| 27 | it("queued exclusive", () => { |
| 28 | expect(finished).toBe(true); |
| 29 | }, { exclusive: true }); |
| 30 | }); |
| 31 | `, |
| 32 | ); |
| 33 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0"], { |
| 34 | cwd: root, |
| 35 | stdout: "pipe", |
| 36 | stderr: "pipe", |
| 37 | env: { ...process.env, NO_COLOR: "1" }, |
| 38 | }); |
| 39 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 40 | child.exited, |
| 41 | new Response(child.stdout).text(), |
| 42 | new Response(child.stderr).text(), |
| 43 | ]); |
| 44 | expect(exitCode).toBe(0); |
| 45 | const duration = `${stdout}\n${stderr}`.match(/queued exclusive \((\d+)ms\)/); |
| 46 | expect(duration).not.toBeNull(); |
| 47 | expect(Number(duration![1])).toBeLessThan(250); |
| 48 | } finally { |
| 49 | await rm(root, { recursive: true, force: true }); |
| 50 | } |
| 51 | }, |
| 52 | { tags: ["unit", "local"] }, |
| 53 | ); |
| 54 | |
| 55 | it( |
| 56 | "later tests overtake a waiting exclusive test, which then runs alone", |
| 57 | async () => { |
| 58 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-exclusive-")); |
| 59 | try { |
| 60 | await writeFile( |
| 61 | resolve(root, "exclusive.test.ts"), |
| 62 | ` |
| 63 | import { describe, it, expect } from ${JSON.stringify(apiUrl)}; |
| 64 | describe.concurrent("exclusive", () => { |
| 65 | let running = 0; |
| 66 | let laterStarted = false; |
| 67 | it("holder", async () => { |
| 68 | running++; |
| 69 | await new Promise(resolve => setTimeout(resolve, 300)); |
| 70 | running--; |
| 71 | }); |
| 72 | it("waiting exclusive", () => { |
| 73 | expect(laterStarted).toBe(true); |
| 74 | expect(running).toBe(0); |
| 75 | }, { exclusive: true }); |
| 76 | it("later", async () => { |
| 77 | running++; |
| 78 | laterStarted = true; |
| 79 | await new Promise(resolve => setTimeout(resolve, 300)); |
| 80 | running--; |
| 81 | }); |
| 82 | }); |
| 83 | `, |
| 84 | ); |
| 85 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0"], { |
| 86 | cwd: root, |
| 87 | stdout: "pipe", |
| 88 | stderr: "pipe", |
| 89 | env: { ...process.env, NO_COLOR: "1" }, |
| 90 | }); |
| 91 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 92 | child.exited, |
| 93 | new Response(child.stdout).text(), |
| 94 | new Response(child.stderr).text(), |
| 95 | ]); |
| 96 | expect(`${stdout}\n${stderr}`).toContain("3 passed"); |
| 97 | expect(exitCode).toBe(0); |
| 98 | } finally { |
| 99 | await rm(root, { recursive: true, force: true }); |
| 100 | } |
| 101 | }, |
| 102 | { tags: ["unit", "local"] }, |
| 103 | ); |
| 104 | |
| 105 | it( |
| 106 | "exclusive labels serialize holders of the same label and nothing else", |
| 107 | async () => { |
| 108 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-labels-")); |
| 109 | try { |
| 110 | await writeFile( |
| 111 | resolve(root, "labels.test.ts"), |
| 112 | ` |
| 113 | import { describe, it, expect } from ${JSON.stringify(apiUrl)}; |
| 114 | const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
| 115 | describe.concurrent("labels", () => { |
| 116 | let activeA = 0; |
| 117 | const holdA = async () => { |
| 118 | activeA++; |
| 119 | expect(activeA).toBe(1); |
| 120 | await sleep(200); |
| 121 | activeA--; |
| 122 | }; |
| 123 | it("a1", holdA, { exclusive: "a" }); |
| 124 | it("a2", holdA, { exclusive: "a" }); |
| 125 | it("b", async () => { |
| 126 | await sleep(50); |
| 127 | expect(activeA).toBe(1); |
| 128 | }, { exclusive: "b" }); |
| 129 | it("unlabeled", async () => { |
| 130 | await sleep(50); |
| 131 | expect(activeA).toBe(1); |
| 132 | }); |
| 133 | }); |
| 134 | `, |
| 135 | ); |
| 136 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0"], { |
| 137 | cwd: root, |
| 138 | stdout: "pipe", |
| 139 | stderr: "pipe", |
| 140 | env: { ...process.env, NO_COLOR: "1" }, |
| 141 | }); |
| 142 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 143 | child.exited, |
| 144 | new Response(child.stdout).text(), |
| 145 | new Response(child.stderr).text(), |
| 146 | ]); |
| 147 | expect(`${stdout}\n${stderr}`).toContain("4 passed"); |
| 148 | expect(exitCode).toBe(0); |
| 149 | } finally { |
| 150 | await rm(root, { recursive: true, force: true }); |
| 151 | } |
| 152 | }, |
| 153 | { tags: ["unit", "local"] }, |
| 154 | ); |
| 155 | |
| 156 | it( |
| 157 | "tag filters compose with names and skip excluded setup and teardown", |
| 158 | async () => { |
| 159 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-tags-")); |
| 160 | try { |
| 161 | await writeFile( |
| 162 | resolve(root, "tags.test.ts"), |
| 163 | ` |
| 164 | import { describe, it, beforeAll, afterAll, expect } from ${JSON.stringify(apiUrl)}; |
| 165 | let ready = false; |
| 166 | let ran = false; |
| 167 | describe("selected", { tags: ["e2e", "provider:aws"] }, () => { |
| 168 | beforeAll(() => { ready = true; }); |
| 169 | afterAll(() => { expect(ran).toBe(true); }); |
| 170 | describe("nested", { tags: ["live"] }, () => { |
| 171 | it("chosen", () => { expect(ready).toBe(true); ran = true; }); |
| 172 | it("slow", () => { throw new Error("slow body ran"); }, { tags: "slow" }); |
| 173 | it("other-name", () => { throw new Error("name filter ignored"); }); |
| 174 | }); |
| 175 | }); |
| 176 | describe("excluded", { tags: ["e2e", "dev", "provider:cloudflare"] }, () => { |
| 177 | beforeAll(() => { throw new Error("excluded setup ran"); }); |
| 178 | afterAll(() => { throw new Error("excluded teardown ran"); }); |
| 179 | it("chosen", () => { throw new Error("excluded body ran"); }); |
| 180 | }); |
| 181 | it("untagged", () => { throw new Error("untagged body ran"); }); |
| 182 | `, |
| 183 | ); |
| 184 | const run = async (args: string[]) => { |
| 185 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", ...args], { |
| 186 | cwd: root, |
| 187 | stdout: "pipe", |
| 188 | stderr: "pipe", |
| 189 | env: { ...process.env, NO_COLOR: "1" }, |
| 190 | }); |
| 191 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 192 | child.exited, |
| 193 | new Response(child.stdout).text(), |
| 194 | new Response(child.stderr).text(), |
| 195 | ]); |
| 196 | return { exitCode, output: `${stdout}\n${stderr}` }; |
| 197 | }; |
| 198 | await writeFile( |
| 199 | resolve(root, "excluded.test.ts"), |
| 200 | ` |
| 201 | import { it, beforeAll, afterAll } from ${JSON.stringify(apiUrl)}; |
| 202 | beforeAll(() => { throw new Error("excluded file setup ran"); }); |
| 203 | afterAll(() => { throw new Error("excluded file teardown ran"); }); |
| 204 | it("excluded", () => { throw new Error("excluded file body ran"); }, { tags: "unit" }); |
| 205 | `, |
| 206 | ); |
| 207 | const selected = await run([ |
| 208 | "--tags", |
| 209 | "e2e && live && provider:*", |
| 210 | "--tags", |
| 211 | "!slow", |
| 212 | "-t", |
| 213 | "chosen", |
| 214 | ]); |
| 215 | expect(selected.exitCode).toBe(0); |
| 216 | expect(selected.output).toContain("1 passed"); |
| 217 | expect(selected.output).toContain("selected > nested > chosen"); |
| 218 | const empty = await run(["--tags", "missing"]); |
| 219 | expect(empty.exitCode).toBe(0); |
| 220 | expect(empty.output).toContain("0 passed"); |
| 221 | |
| 222 | await writeFile( |
| 223 | resolve(root, "only.test.ts"), |
| 224 | ` |
| 225 | import { it } from ${JSON.stringify(apiUrl)}; |
| 226 | it.only("only", () => { throw new Error("only bypassed tags"); }, { tags: "other" }); |
| 227 | `, |
| 228 | ); |
| 229 | const only = await run(["--tags", "e2e"]); |
| 230 | expect(only.exitCode).toBe(0); |
| 231 | expect(only.output).toContain("0 passed"); |
| 232 | |
| 233 | // Even an import that throws must not be reached with malformed syntax. |
| 234 | await writeFile(resolve(root, "bad.test.ts"), 'throw new Error("IMPORTED_SENTINEL");'); |
| 235 | const invalid = await run(["--tags", "unit &&"]); |
| 236 | expect(invalid.exitCode).not.toBe(0); |
| 237 | expect(invalid.output).toContain("Invalid --tags"); |
| 238 | expect(invalid.output).not.toContain("IMPORTED_SENTINEL"); |
| 239 | } finally { |
| 240 | await rm(root, { recursive: true, force: true }); |
| 241 | } |
| 242 | }, |
| 243 | { tags: ["unit", "local"] }, |
| 244 | ); |
| 245 | |
| 246 | const fixture = (hook: string, body: string): string => ` |
| 247 | import { it, registerHook } from ${JSON.stringify(apiUrl)}; |
| 248 | import * as Effect from ${JSON.stringify(effectUrl)}; |
| 249 | registerHook(${JSON.stringify(hook)}, { body: () => Effect.gen(function* () { |
| 250 | yield* Effect.log(${JSON.stringify(`${hook}-captured-output`)}); |
| 251 | return yield* Effect.fail(new Error(${JSON.stringify(`${hook}-sentinel`)})); |
| 252 | }) |
| 253 | }); |
| 254 | ${body} |
| 255 | `; |
| 256 | |
| 257 | it( |
| 258 | "streams file-hook output to the run log while the hook is still running", |
| 259 | async () => { |
| 260 | // Regression: file-level hook output (deploy/destroy) used to be buffered |
| 261 | // until FileEnd, so a long-running beforeAll produced a run log that |
| 262 | // stopped growing entirely — a multi-minute cloud deploy read as a |
| 263 | // deadlocked run (0% CPU, silent log). Hook log entries must reach the |
| 264 | // per-run log file WHILE the hook is still executing. |
| 265 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-livehook-")); |
| 266 | try { |
| 267 | await writeFile( |
| 268 | resolve(root, "live-hook.test.ts"), |
| 269 | ` |
| 270 | import { it, registerHook } from ${JSON.stringify(apiUrl)}; |
| 271 | import * as Effect from ${JSON.stringify(effectUrl)}; |
| 272 | registerHook("beforeAll", { body: () => Effect.gen(function* () { |
| 273 | yield* Effect.log("hook-live-sentinel"); |
| 274 | yield* Effect.sleep("8 seconds"); |
| 275 | }) }); |
| 276 | it("body", () => {}); |
| 277 | `, |
| 278 | ); |
| 279 | |
| 280 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", "--concurrency", "1"], { |
| 281 | cwd: root, |
| 282 | stdout: "pipe", |
| 283 | stderr: "pipe", |
| 284 | env: { ...process.env, NO_COLOR: "1" }, |
| 285 | }); |
| 286 | try { |
| 287 | // Poll the run log (created under the child's cwd) for the sentinel. |
| 288 | // The hook sleeps 8s after logging; seeing the sentinel within ~6s |
| 289 | // proves it was streamed mid-hook, not flushed at FileEnd. |
| 290 | const logDir = resolve(root, ".alchemy", "log", "test"); |
| 291 | const deadline = Date.now() + 6_000; |
| 292 | let streamed = false; |
| 293 | while (Date.now() < deadline) { |
| 294 | const { readdir, readFile } = await import("node:fs/promises"); |
| 295 | const entries = await readdir(logDir).catch(() => [] as string[]); |
| 296 | for (const entry of entries) { |
| 297 | const content = await readFile(resolve(logDir, entry), "utf8").catch(() => ""); |
| 298 | if (content.includes("hook-live-sentinel")) { |
| 299 | streamed = true; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (streamed) break; |
| 304 | await new Promise((r) => setTimeout(r, 200)); |
| 305 | } |
| 306 | expect(streamed).toBe(true); |
| 307 | |
| 308 | const exitCode = await child.exited; |
| 309 | expect(exitCode).toBe(0); |
| 310 | } finally { |
| 311 | child.kill(); |
| 312 | } |
| 313 | } finally { |
| 314 | await rm(root, { recursive: true, force: true }); |
| 315 | } |
| 316 | }, |
| 317 | { tags: ["unit", "local"] }, |
| 318 | ); |
| 319 | |
| 320 | it( |
| 321 | "--exclude skips folders unless they are passed explicitly", |
| 322 | async () => { |
| 323 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-exclude-")); |
| 324 | try { |
| 325 | const { mkdir } = await import("node:fs/promises"); |
| 326 | await mkdir(resolve(root, "test", "Railway"), { recursive: true }); |
| 327 | await mkdir(resolve(root, "test", "Other"), { recursive: true }); |
| 328 | const testFile = (name: string) => ` |
| 329 | import { it } from ${JSON.stringify(apiUrl)}; |
| 330 | it(${JSON.stringify(name)}, () => {}); |
| 331 | `; |
| 332 | await Promise.all([ |
| 333 | writeFile(resolve(root, "test", "Railway", "Excluded.test.ts"), testFile("excluded-test")), |
| 334 | writeFile(resolve(root, "test", "Other", "Included.test.ts"), testFile("included-test")), |
| 335 | ]); |
| 336 | |
| 337 | const runCli = async (args: ReadonlyArray<string>) => { |
| 338 | const child = Bun.spawn( |
| 339 | [process.execPath, cli, ...args, "--retry", "0", "--concurrency", "1"], |
| 340 | { cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, NO_COLOR: "1" } }, |
| 341 | ); |
| 342 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 343 | child.exited, |
| 344 | new Response(child.stdout).text(), |
| 345 | new Response(child.stderr).text(), |
| 346 | ]); |
| 347 | return { exitCode, output: `${stdout}\n${stderr}` }; |
| 348 | }; |
| 349 | |
| 350 | // Default discovery with the exclusion: only the non-excluded file runs. |
| 351 | const excluded = await runCli(["--exclude", "test/Railway"]); |
| 352 | expect(excluded.exitCode).toBe(0); |
| 353 | expect(excluded.output).toContain("included-test"); |
| 354 | expect(excluded.output).not.toContain("excluded-test"); |
| 355 | |
| 356 | // An explicit positional root inside the excluded path overrides it. |
| 357 | const explicit = await runCli(["test/Railway/Excluded.test.ts", "--exclude", "test/Railway"]); |
| 358 | expect(explicit.exitCode).toBe(0); |
| 359 | expect(explicit.output).toContain("excluded-test"); |
| 360 | |
| 361 | // Non-path excludes degrade to case-insensitive substring filters. |
| 362 | const substring = await runCli(["--exclude", "railway"]); |
| 363 | expect(substring.exitCode).toBe(0); |
| 364 | expect(substring.output).toContain("included-test"); |
| 365 | expect(substring.output).not.toContain("excluded-test"); |
| 366 | } finally { |
| 367 | await rm(root, { recursive: true, force: true }); |
| 368 | } |
| 369 | }, |
| 370 | { tags: ["unit", "local"] }, |
| 371 | ); |
| 372 | |
| 373 | it( |
| 374 | "fails the process for every hook kind and preserves hook output", |
| 375 | async () => { |
| 376 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-hooks-")); |
| 377 | try { |
| 378 | await Promise.all([ |
| 379 | writeFile( |
| 380 | resolve(root, "before-all.test.ts"), |
| 381 | fixture("beforeAll", 'it("body", () => {});'), |
| 382 | ), |
| 383 | writeFile( |
| 384 | resolve(root, "before-each.test.ts"), |
| 385 | fixture("beforeEach", 'it("body", () => {});'), |
| 386 | ), |
| 387 | writeFile( |
| 388 | resolve(root, "after-each.test.ts"), |
| 389 | fixture( |
| 390 | "afterEach", |
| 391 | 'it.fails("expected body failure", () => { throw new Error("expected-body-failure"); });', |
| 392 | ), |
| 393 | ), |
| 394 | writeFile(resolve(root, "after-all.test.ts"), fixture("afterAll", 'it("body", () => {});')), |
| 395 | ]); |
| 396 | |
| 397 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", "--concurrency", "1"], { |
| 398 | cwd: root, |
| 399 | stdout: "pipe", |
| 400 | stderr: "pipe", |
| 401 | env: { ...process.env, NO_COLOR: "1" }, |
| 402 | }); |
| 403 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 404 | child.exited, |
| 405 | new Response(child.stdout).text(), |
| 406 | new Response(child.stderr).text(), |
| 407 | ]); |
| 408 | const output = `${stdout}\n${stderr}`; |
| 409 | |
| 410 | expect(exitCode).toBe(1); |
| 411 | expect(output).toContain("beforeAll hook failed:"); |
| 412 | expect(output).toContain("beforeEach hook failed:"); |
| 413 | expect(output).toContain("afterEach hook failed:"); |
| 414 | expect(output).toContain("afterAll hook failed:"); |
| 415 | expect(output).toContain("Tests: 4 failed | 1 passed"); |
| 416 | for (const hook of ["beforeAll", "beforeEach", "afterEach", "afterAll"]) { |
| 417 | expect(output).toContain(`${hook}-captured-output`); |
| 418 | expect(output).toContain(`${hook}-sentinel`); |
| 419 | } |
| 420 | } finally { |
| 421 | await rm(root, { recursive: true, force: true }); |
| 422 | } |
| 423 | }, |
| 424 | { tags: ["unit", "local"] }, |
| 425 | ); |
| 426 | |
| 427 | it( |
| 428 | "runs every afterAll hook even when an earlier one fails", |
| 429 | async () => { |
| 430 | // Regression: `runAfterAll` used to short-circuit on the first failing |
| 431 | // hook, so a failing teardown assertion silently dropped every later |
| 432 | // afterAll — in particular Test.make's fallback hook that closes the |
| 433 | // shared scope and local provider sidecar, leaking the sidecar for the |
| 434 | // rest of the process. All teardown hooks must run; failures aggregate. |
| 435 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-afterall-")); |
| 436 | try { |
| 437 | await writeFile( |
| 438 | resolve(root, "teardown-chain.test.ts"), |
| 439 | ` |
| 440 | import { it, registerHook } from ${JSON.stringify(apiUrl)}; |
| 441 | import * as Effect from ${JSON.stringify(effectUrl)}; |
| 442 | registerHook("afterAll", { body: () => Effect.gen(function* () { |
| 443 | return yield* Effect.fail(new Error("first-teardown-failed")); |
| 444 | }) }); |
| 445 | registerHook("afterAll", { body: () => Effect.gen(function* () { |
| 446 | yield* Effect.log("second-teardown-ran"); |
| 447 | }) }); |
| 448 | it("body", () => {}); |
| 449 | `, |
| 450 | ); |
| 451 | |
| 452 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", "--concurrency", "1"], { |
| 453 | cwd: root, |
| 454 | stdout: "pipe", |
| 455 | stderr: "pipe", |
| 456 | env: { ...process.env, NO_COLOR: "1" }, |
| 457 | }); |
| 458 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 459 | child.exited, |
| 460 | new Response(child.stdout).text(), |
| 461 | new Response(child.stderr).text(), |
| 462 | ]); |
| 463 | const output = `${stdout}\n${stderr}`; |
| 464 | |
| 465 | // The failure is reported and fails the run… |
| 466 | expect(exitCode).toBe(1); |
| 467 | expect(output).toContain("afterAll hook failed:"); |
| 468 | expect(output).toContain("first-teardown-failed"); |
| 469 | // …and the later teardown hook still ran. |
| 470 | expect(output).toContain("second-teardown-ran"); |
| 471 | } finally { |
| 472 | await rm(root, { recursive: true, force: true }); |
| 473 | } |
| 474 | }, |
| 475 | { tags: ["unit", "local"] }, |
| 476 | ); |
| 477 | |
| 478 | it( |
| 479 | "attributes imported tests and deferred hooks to their own files", |
| 480 | async () => { |
| 481 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-collection-")); |
| 482 | try { |
| 483 | for (const name of ["first", "second"]) { |
| 484 | const file = `${name}.test.ts`; |
| 485 | await writeFile( |
| 486 | resolve(root, file), |
| 487 | ` |
| 488 | import { beforeAll, afterAll, currentFile, describe, expect, it } from ${JSON.stringify(apiUrl)}; |
| 489 | expect(currentFile()?.split("/").pop()).toBe(${JSON.stringify(file)}); |
| 490 | await new Promise(resolve => setTimeout(resolve, 10)); |
| 491 | expect(currentFile()?.split("/").pop()).toBe(${JSON.stringify(file)}); |
| 492 | let ready = false; |
| 493 | let ran = false; |
| 494 | queueMicrotask(() => { |
| 495 | expect(currentFile()?.split("/").pop()).toBe(${JSON.stringify(file)}); |
| 496 | beforeAll(() => { ready = true; }); |
| 497 | afterAll(() => { expect(ran).toBe(true); }); |
| 498 | }); |
| 499 | describe("nested", () => { |
| 500 | it(${JSON.stringify(name)}, () => { |
| 501 | expect(currentFile()).toBeUndefined(); |
| 502 | expect(ready).toBe(true); |
| 503 | ran = true; |
| 504 | }); |
| 505 | }); |
| 506 | `, |
| 507 | ); |
| 508 | } |
| 509 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", "--concurrency", "2"], { |
| 510 | cwd: root, |
| 511 | stdout: "pipe", |
| 512 | stderr: "pipe", |
| 513 | env: { ...process.env, NO_COLOR: "1" }, |
| 514 | }); |
| 515 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 516 | child.exited, |
| 517 | new Response(child.stdout).text(), |
| 518 | new Response(child.stderr).text(), |
| 519 | ]); |
| 520 | expect(`${stdout}\n${stderr}`).toContain("first.test.ts > nested > first"); |
| 521 | expect(`${stdout}\n${stderr}`).toContain("second.test.ts > nested > second"); |
| 522 | expect(exitCode).toBe(0); |
| 523 | } finally { |
| 524 | await rm(root, { recursive: true, force: true }); |
| 525 | } |
| 526 | }, |
| 527 | { tags: ["unit", "local"] }, |
| 528 | ); |
| 529 | |
| 530 | it( |
| 531 | "opt-in tags gate execution and hooks until explicitly selected", |
| 532 | async () => { |
| 533 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-opt-in-")); |
| 534 | const file = resolve(root, "opt-in.test.ts"); |
| 535 | try { |
| 536 | await writeFile( |
| 537 | file, |
| 538 | ` |
| 539 | import { describe, it, beforeAll, afterAll, expect } from ${JSON.stringify(apiUrl)}; |
| 540 | let ran = false; |
| 541 | describe("gated", { tags: ["e2e", "provider:aws"], optInTags: ["slow"] }, () => { |
| 542 | beforeAll(() => { if (!process.env.EXPECT_ENABLED) throw new Error("excluded setup ran"); }); |
| 543 | afterAll(() => { expect(ran).toBe(true); }); |
| 544 | it("single", () => { ran = true; }); |
| 545 | it("double", () => { ran = true; }, { optInTags: ["enterprise"] }); |
| 546 | }); |
| 547 | it("ordinary", () => {}, { tags: ["unit", "slow"] }); |
| 548 | `, |
| 549 | ); |
| 550 | const run = async (args: string[], enabled = false) => { |
| 551 | const child = Bun.spawn([process.execPath, cli, file, "--retry", "0", ...args], { |
| 552 | cwd: root, |
| 553 | stdout: "pipe", |
| 554 | stderr: "pipe", |
| 555 | env: { ...process.env, NO_COLOR: "1", EXPECT_ENABLED: enabled ? "1" : "" }, |
| 556 | }); |
| 557 | const [code, stdout, stderr] = await Promise.all([ |
| 558 | child.exited, |
| 559 | new Response(child.stdout).text(), |
| 560 | new Response(child.stderr).text(), |
| 561 | ]); |
| 562 | expect(code).toBe(0); |
| 563 | return `${stdout}\n${stderr}`; |
| 564 | }; |
| 565 | expect(await run([])).toContain("1 passed"); |
| 566 | expect(await run(["--tags", "*"])).toContain("1 passed"); |
| 567 | for (const filter of ["provider:aws", "!unit", "sl*", "!!slow"]) { |
| 568 | expect(await run(["--tags", filter, "-t", "gated"])).toContain("0 passed"); |
| 569 | } |
| 570 | expect(await run(["-t", "gated"])).toContain("0 passed"); |
| 571 | expect(await run(["--tags", "slow && provider:aws"], true)).toContain("1 passed"); |
| 572 | expect(await run(["--tags", "slow", "--tags", "enterprise"], true)).toContain("1 passed"); |
| 573 | await writeFile( |
| 574 | file, |
| 575 | ` |
| 576 | import { it } from ${JSON.stringify(apiUrl)}; |
| 577 | it.only("gated", () => { throw new Error("only bypassed opt-in"); }, { optInTags: ["slow"] }); |
| 578 | `, |
| 579 | ); |
| 580 | expect(await run([])).toContain("0 passed"); |
| 581 | } finally { |
| 582 | await rm(root, { recursive: true, force: true }); |
| 583 | } |
| 584 | }, |
| 585 | { tags: ["unit", "local"], timeout: 90_000 }, |
| 586 | ); |
| 587 | |
| 588 | it( |
| 589 | "fails and abandons test bodies, retries and hooks that outlive their timeout", |
| 590 | async () => { |
| 591 | const root = await mkdtemp(resolve(tmpdir(), "alchemy-test-deadline-")); |
| 592 | const header = ` |
| 593 | import { describe, it, registerHook } from ${JSON.stringify(apiUrl)}; |
| 594 | import * as Effect from ${JSON.stringify(effectUrl)}; |
| 595 | const hang = () => Effect.uninterruptible(Effect.never); |
| 596 | `; |
| 597 | const hookFixture = (kind: string, timeout = ", timeout: 300") => `${header} |
| 598 | describe(${JSON.stringify(kind)}, () => { |
| 599 | registerHook(${JSON.stringify(kind)}, { body: hang${timeout} }); |
| 600 | it("body", () => {}); |
| 601 | }); |
| 602 | `; |
| 603 | try { |
| 604 | await Promise.all([ |
| 605 | writeFile( |
| 606 | resolve(root, "body.test.ts"), |
| 607 | `${header} |
| 608 | it.effect("uninterruptible body", hang, { timeout: 300 }); |
| 609 | it("never-resolving promise body", () => new Promise(() => {}), { timeout: 300 }); |
| 610 | it.effect("hung finalizer body", () => |
| 611 | Effect.addFinalizer(() => Effect.never).pipe(Effect.andThen(Effect.never)), |
| 612 | { timeout: 300 }); |
| 613 | it.effect("retried uninterruptible body", hang, { timeout: 300, retry: 2 }); |
| 614 | `, |
| 615 | ), |
| 616 | writeFile(resolve(root, "before-all.test.ts"), hookFixture("beforeAll")), |
| 617 | writeFile(resolve(root, "before-each.test.ts"), hookFixture("beforeEach")), |
| 618 | writeFile(resolve(root, "after-each.test.ts"), hookFixture("afterEach")), |
| 619 | // No hook timeout: falls back to the run's --timeout. |
| 620 | writeFile(resolve(root, "after-all.test.ts"), hookFixture("afterAll", "")), |
| 621 | ]); |
| 622 | |
| 623 | const started = Date.now(); |
| 624 | const child = Bun.spawn([process.execPath, cli, root, "--retry", "0", "--timeout", "300"], { |
| 625 | cwd: root, |
| 626 | stdout: "pipe", |
| 627 | stderr: "pipe", |
| 628 | env: { ...process.env, NO_COLOR: "1", ALCHEMY_TEST_TEARDOWN_GRACE_MS: "500" }, |
| 629 | // A regression hangs forever; kill it so the assertion fails instead. |
| 630 | timeout: 60_000, |
| 631 | killSignal: "SIGKILL", |
| 632 | }); |
| 633 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 634 | child.exited, |
| 635 | new Response(child.stdout).text(), |
| 636 | new Response(child.stderr).text(), |
| 637 | ]); |
| 638 | const elapsed = Date.now() - started; |
| 639 | const output = `${stdout}\n${stderr}`; |
| 640 | |
| 641 | expect(exitCode).toBe(1); |
| 642 | const abandoned = "(teardown did not settle within 500ms and was abandoned)"; |
| 643 | expect(output).toContain(`test timed out after 300ms ${abandoned}`); |
| 644 | expect(output).toContain("test timed out after 300ms\n"); |
| 645 | expect(output).toContain(`hook timed out after 300ms ${abandoned}`); |
| 646 | for (const hook of ["beforeAll", "beforeEach", "afterEach", "afterAll"]) { |
| 647 | expect(output).toContain(`${hook} hook failed:`); |
| 648 | } |
| 649 | expect(output).toMatch(/retried uninterruptible body \([\d.]+m?s\) \[retried x2\]/); |
| 650 | expect(output).toContain("Tests: 8 failed | 1 passed"); |
| 651 | // Every hang is bounded by (timeout + grace) per attempt. |
| 652 | expect(elapsed).toBeLessThan(30_000); |
| 653 | } finally { |
| 654 | await rm(root, { recursive: true, force: true }); |
| 655 | } |
| 656 | }, |
| 657 | { tags: ["unit", "local"], timeout: 90_000 }, |
| 658 | ); |