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

fix: improve fixture test reliability/debugability #3863

Merged
merged 1 commit into from
Aug 29, 2023
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
14 changes: 8 additions & 6 deletions fixtures/node-app-pages/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ describe("Pages Dev", () => {
"public",
["--node-compat", "--port=0"]
);
const response = await fetch(`http://${ip}:${port}/stripe`);
try {
const response = await fetch(`http://${ip}:${port}/stripe`);

await expect(response.text()).resolves.toContain(
`"PATH":"path/to/some-file","STRIPE_OBJECT"`
);

await stop();
await expect(response.text()).resolves.toContain(
`"PATH":"path/to/some-file","STRIPE_OBJECT"`
);
} finally {
await stop();
}
});
});
4 changes: 2 additions & 2 deletions fixtures/pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Functions", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -20,7 +20,7 @@ describe("Pages Functions", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("renders static pages", async ({ expect }) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-functions-wasm-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Functions with wasm module imports", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages Functions with wasm module imports", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("should render static pages", async ({ expect }) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-functions-with-routes-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Functions with custom _routes.json", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages Functions with custom _routes.json", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("should render static pages", async ({ expect }) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-simple-assets/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, afterAll, beforeAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Functions", async () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages Functions", async () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("renders static pages", async ({ expect }) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-workerjs-and-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages project with `_worker.js` and `/functions` directory", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages project with `_worker.js` and `/functions` directory", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("renders static pages", async ({ expect }) => {
Expand Down
22 changes: 14 additions & 8 deletions fixtures/pages-workerjs-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ describe("Pages _worker.js", () => {
"./workerjs-test",
["--no-bundle=false", "--port=0"]
);
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("test");
await stop();
try {
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("test");
} finally {
await stop();
}
});

it("should not throw an error when the _worker.js file imports something if --bundle is true", async ({
Expand All @@ -49,9 +52,12 @@ describe("Pages _worker.js", () => {
"./workerjs-test",
["--bundle", "--port=0"]
);
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("test");
await stop();
try {
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("test");
} finally {
await stop();
}
});
});
65 changes: 35 additions & 30 deletions fixtures/pages-workerjs-directory/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,41 @@ describe("Pages _worker.js/ directory", () => {
"--r2=R2_REF=other_r2",
]
);
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("Hello, world!");
await expect(
fetch(`http://${ip}:${port}/wasm`).then((resp) => resp.text())
).resolves.toContain("3");
await expect(
fetch(`http://${ip}:${port}/static-js`).then((resp) => resp.text())
).resolves.toEqual("static import text (via js): 'js static'");
await expect(
fetch(`http://${ip}:${port}/static-mjs`).then((resp) => resp.text())
).resolves.toEqual("static import text (via mjs): 'mjs static'");
await expect(
fetch(`http://${ip}:${port}/other-script.js`).then((resp) => resp.text())
).resolves.toContain("other-script-test");
await expect(
fetch(`http://${ip}:${port}/other-other-script.mjs`).then((resp) =>
resp.text()
)
).resolves.toContain("other-other-script-test");
await expect(
fetch(`http://${ip}:${port}/d1`).then((resp) => resp.text())
).resolves.toContain('{"1":1}');
await expect(
fetch(`http://${ip}:${port}/kv`).then((resp) => resp.text())
).resolves.toContain("saved");
await expect(
fetch(`http://${ip}:${port}/r2`).then((resp) => resp.text())
).resolves.toContain("saved");
await stop();
try {
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("Hello, world!");
await expect(
fetch(`http://${ip}:${port}/wasm`).then((resp) => resp.text())
).resolves.toContain("3");
await expect(
fetch(`http://${ip}:${port}/static-js`).then((resp) => resp.text())
).resolves.toEqual("static import text (via js): 'js static'");
await expect(
fetch(`http://${ip}:${port}/static-mjs`).then((resp) => resp.text())
).resolves.toEqual("static import text (via mjs): 'mjs static'");
await expect(
fetch(`http://${ip}:${port}/other-script.js`).then((resp) =>
resp.text()
)
).resolves.toContain("other-script-test");
await expect(
fetch(`http://${ip}:${port}/other-other-script.mjs`).then((resp) =>
resp.text()
)
).resolves.toContain("other-other-script-test");
await expect(
fetch(`http://${ip}:${port}/d1`).then((resp) => resp.text())
).resolves.toContain('{"1":1}');
await expect(
fetch(`http://${ip}:${port}/kv`).then((resp) => resp.text())
).resolves.toContain("saved");
await expect(
fetch(`http://${ip}:${port}/r2`).then((resp) => resp.text())
).resolves.toContain("saved");
} finally {
await stop();
}

expect(existsSync(join(tmpDir, "./v3/d1/D1"))).toBeTruthy();
expect(existsSync(join(tmpDir, "./v3/d1/elsewhere"))).toBeTruthy();
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-workerjs-wasm-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Advanced Mode with wasm module imports", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages Advanced Mode with wasm module imports", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("should render static pages", async ({ expect }) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/pages-workerjs-with-routes-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages Advanced Mode with custom _routes.json", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerPagesDev(
Expand All @@ -15,7 +15,7 @@ describe("Pages Advanced Mode with custom _routes.json", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("renders static pages", async ({ expect }) => {
Expand Down
6 changes: 4 additions & 2 deletions fixtures/remix-pages-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const isWindows = process.platform === "win32";
describe("Remix", () => {
let ip: string;
let port: number;
let stop: () => void;
let stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
spawnSync("npm", ["run", "build"], {
Expand All @@ -23,7 +23,9 @@ describe("Remix", () => {
));
});

afterAll(async () => await stop());
afterAll(async () => {
await stop?.();
});

it("renders", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
Expand Down
33 changes: 27 additions & 6 deletions fixtures/shared/src/run-wrangler-long-lived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,46 @@ export async function runWranglerDev(cwd: string, options: string[]) {
}

async function runLongLivedWrangler(command: string[], cwd: string) {
let settledReadyPromise = false;
let resolveReadyPromise: (value: { ip: string; port: number }) => void;
let rejectReadyPromise: (reason: unknown) => void;

const ready = new Promise<{ ip: string; port: number }>(
(resolve) => (resolveReadyPromise = resolve)
);
const ready = new Promise<{ ip: string; port: number }>((resolve, reject) => {
resolveReadyPromise = resolve;
rejectReadyPromise = reject;
});

const wranglerProcess = fork(
"../../packages/wrangler/bin/wrangler.js",
command,
{
stdio: ["ignore", "ignore", "ignore", "ipc"],
// If you have a test timing out unexpectedly, then try using this line to find out what is happening in Wrangler.
// stdio: ["inherit", "inherit", "inherit", "ipc"],
stdio: [/*stdin*/ "ignore", /*stdout*/ "pipe", /*stderr*/ "pipe", "ipc"],
cwd,
}
).on("message", (message) => {
if (settledReadyPromise) return;
settledReadyPromise = true;
clearTimeout(timeoutHandle);
resolveReadyPromise(JSON.parse(message.toString()));
});

const chunks: Buffer[] = [];
wranglerProcess.stdout?.on("data", (chunk) => chunks.push(chunk));
wranglerProcess.stderr?.on("data", (chunk) => chunks.push(chunk));

const timeoutHandle = setTimeout(() => {
if (settledReadyPromise) return;
settledReadyPromise = true;
const separator = "=".repeat(80);
const message = [
"Timed out starting long-lived Wrangler:",
separator,
Buffer.concat(chunks).toString(),
separator,
].join("\n");
rejectReadyPromise(new Error(message));
}, 10_000);

async function stop() {
return new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/worker-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";

describe("'wrangler dev' correctly renders pages", () => {
let ip: string, port: number, stop: () => Promise<unknown>;
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;

beforeAll(async () => {
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [
Expand All @@ -14,7 +14,7 @@ describe("'wrangler dev' correctly renders pages", () => {
});

afterAll(async () => {
await stop();
await stop?.();
});

it("renders ", async ({ expect }) => {
Expand Down