|
| 1 | +import dedent from "ts-dedent"; |
| 2 | +import { test, waitFor } from "./helpers"; |
| 3 | + |
| 4 | +test("opens an inspector with the `--inspect` argument", async ({ |
| 5 | + expect, |
| 6 | + seed, |
| 7 | + vitestDev, |
| 8 | +}) => { |
| 9 | + await seed({ |
| 10 | + "vitest.config.mts": dedent` |
| 11 | + import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; |
| 12 | + export default defineWorkersConfig({ |
| 13 | + test: { |
| 14 | + poolOptions: { |
| 15 | + workers: { |
| 16 | + main: "./index.ts", |
| 17 | + singleWorker: true, |
| 18 | + miniflare: { |
| 19 | + compatibilityDate: "2024-01-01", |
| 20 | + compatibilityFlags: ["nodejs_compat"], |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + } |
| 25 | + }); |
| 26 | + `, |
| 27 | + "index.ts": dedent` |
| 28 | + export default { |
| 29 | + async fetch(request, env, ctx) { |
| 30 | + return new Response("hello world"); |
| 31 | + } |
| 32 | + } |
| 33 | + `, |
| 34 | + "index.test.ts": dedent` |
| 35 | + import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test"; |
| 36 | + import { it, expect } from "vitest"; |
| 37 | + import worker from "./index"; |
| 38 | + it("sends request", async () => { |
| 39 | + const request = new Request("https://example.com"); |
| 40 | + const ctx = createExecutionContext(); |
| 41 | + const response = await worker.fetch(request, env, ctx); |
| 42 | + await waitOnExecutionContext(ctx); |
| 43 | + expect(await response.text()).toBe("hello world"); |
| 44 | + }); |
| 45 | + `, |
| 46 | + }); |
| 47 | + const result = vitestDev({ |
| 48 | + flags: ["--inspect", "--no-file-parallelism"], |
| 49 | + }); |
| 50 | + |
| 51 | + await waitFor(() => { |
| 52 | + expect(result.stdout).toMatch("inspector on port 9229"); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +test("customize inspector config", async ({ expect, seed, vitestDev }) => { |
| 57 | + await seed({ |
| 58 | + "vitest.config.mts": dedent` |
| 59 | + import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; |
| 60 | + export default defineWorkersConfig({ |
| 61 | + test: { |
| 62 | + inspector: { |
| 63 | + // Test if this overrides the inspector port |
| 64 | + port: 3456, |
| 65 | + }, |
| 66 | + poolOptions: { |
| 67 | + workers: { |
| 68 | + main: "./index.ts", |
| 69 | + // Test if we warn and override the singleWorker option when the inspector is open |
| 70 | + singleWorker: false, |
| 71 | + miniflare: { |
| 72 | + compatibilityDate: "2024-01-01", |
| 73 | + compatibilityFlags: ["nodejs_compat"], |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + }); |
| 79 | + `, |
| 80 | + "index.ts": dedent` |
| 81 | + export default { |
| 82 | + async fetch(request, env, ctx) { |
| 83 | + return new Response("hello world"); |
| 84 | + } |
| 85 | + } |
| 86 | + `, |
| 87 | + "index.test.ts": dedent` |
| 88 | + import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test"; |
| 89 | + import { it, expect } from "vitest"; |
| 90 | + import worker from "./index"; |
| 91 | + it("sends request", async () => { |
| 92 | + const request = new Request("https://example.com"); |
| 93 | + const ctx = createExecutionContext(); |
| 94 | + const response = await worker.fetch(request, env, ctx); |
| 95 | + await waitOnExecutionContext(ctx); |
| 96 | + expect(await response.text()).toBe("hello world"); |
| 97 | + }); |
| 98 | + `, |
| 99 | + }); |
| 100 | + const result = vitestDev({ |
| 101 | + // Test if we warn and ignore the `waitForDebugger` option |
| 102 | + flags: ["--inspect-brk", "--no-file-parallelism"], |
| 103 | + }); |
| 104 | + |
| 105 | + await waitFor(() => { |
| 106 | + expect(result.stdout).toMatch( |
| 107 | + "Tests run in singleWorker mode when the inspector is open." |
| 108 | + ); |
| 109 | + expect(result.stdout).toMatch(`The "--inspect-brk" flag is not supported.`); |
| 110 | + expect(result.stdout).toMatch("Starting single runtime"); |
| 111 | + expect(result.stdout).toMatch("inspector on port 3456"); |
| 112 | + }); |
| 113 | +}); |
0 commit comments