Skip to content

Commit

Permalink
test: remove unwanted logging from Wrangler tests (#6447)
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin authored Sep 6, 2024
1 parent 0c98923 commit c365e61
Show file tree
Hide file tree
Showing 7 changed files with 1,000 additions and 1,006 deletions.
2 changes: 2 additions & 0 deletions packages/wrangler/src/__tests__/api-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as fs from "node:fs";
import { Request } from "undici";
import { vi } from "vitest";
import { unstable_dev } from "../api";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";

vi.unmock("child_process");
vi.unmock("undici");

describe("unstable_dev", () => {
mockConsoleMethods();
it("should return Hello World", async () => {
const worker = await unstable_dev(
"src/__tests__/helpers/worker-scripts/hello-world-worker.js",
Expand Down
30 changes: 8 additions & 22 deletions packages/wrangler/src/__tests__/api-devregistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { mockConsoleMethods } from "./helpers/mock-console";
vi.unmock("child_process");
vi.unmock("undici");

// We need this outside the describe block to make sure the console spies are not
// torn down before the workers.
const std = mockConsoleMethods();

/**
* a huge caveat to how testing multi-worker scripts works:
* you can't shutdown the first worker you spun up, or it'll kill the devRegistry
*/
describe("multi-worker testing", () => {
mockConsoleMethods();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let childWorker: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -80,25 +83,6 @@ describe("multi-worker testing", () => {
});

it("should be able to stop and start the server with no warning logs", async () => {
// Spy on all the console methods
let logs = "";
// Resolve when we see `[mf:inf] GET / 200 OK` message. This log is sent in
// a `waitUntil()`, which may execute after tests complete. To stop Vitest
// complaining about logging after a test, wait for this log.
let requestResolve: () => void;
const requestPromise = new Promise<void>(
(resolve) => (requestResolve = resolve)
);
(["debug", "info", "log", "warn", "error"] as const).forEach((method) =>
vi.spyOn(console, method).mockImplementation((...args: unknown[]) => {
logs += `\n${args}`;
// Regexp ignores colour codes
if (/\[wrangler.*:inf].+GET.+\/.+200.+OK/.test(String(args))) {
requestResolve();
}
})
);

async function startWorker() {
const worker = await unstable_dev(
"src/__tests__/helpers/worker-scripts/hello-world-worker.js",
Expand Down Expand Up @@ -131,9 +115,11 @@ describe("multi-worker testing", () => {
const resp = await worker.fetch();
expect(resp).not.toBe(undefined);

await requestPromise;
await vi.waitFor(() =>
/\[wrangler.*:inf].+GET.+\/.+200.+OK/.test(std.debug)
);

expect(logs).not.toMatch(
expect(std.debug).not.toMatch(
/Failed to register worker in local service registry/
);
}, 10000);
Expand Down
Loading

0 comments on commit c365e61

Please sign in to comment.