Skip to content
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
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"esbuild": "0.28.2",
"figures": "6.1.0",
"minimatch": "10.2.6",
"msw": "2.15.0",
"msw": "3.0.2",
Comment thread
petebacondarwin marked this conversation as resolved.
"semver": "7.8.5",
"shell-quote": "^1.12.0",
"tinyexec": "^1.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "node:fs";
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down
37 changes: 16 additions & 21 deletions packages/cli/src/__tests__/commands/account-selection.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { format } from "node:util";
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down Expand Up @@ -49,27 +48,25 @@ describe("account selection", () => {
runInTempDir();
setupMsw();

const previousDispatcher = getGlobalDispatcher();
let accountsApi: MockAgent;
let terminal: string;

beforeEach(() => {
// workers-auth lists accounts through undici, which MSW doesn't intercept.
accountsApi = new MockAgent();
accountsApi.disableNetConnect();
const api = accountsApi.get("https://api.test");
const page = { page: 1, total_pages: 1 };
api
.intercept({ path: "/client/v4/accounts?page=1" })
.reply(200, { success: true, result: accounts, result_info: page });
api.intercept({ path: "/client/v4/memberships?page=1" }).reply(200, {
success: true,
result: accounts.map((account) => ({ account })),
result_info: page,
});
setGlobalDispatcher(accountsApi);

server.use(
http.get(`${TEST_BASE_URL}/accounts`, () =>
HttpResponse.json({
success: true,
result: accounts,
result_info: page,
})
),
http.get(`${TEST_BASE_URL}/memberships`, () =>
HttpResponse.json({
success: true,
result: accounts.map((account) => ({ account })),
result_info: page,
})
),
http.get(`${TEST_BASE_URL}/accounts/:accountId/pages/projects`, () =>
HttpResponse.json(createFetchResult([]))
)
Expand All @@ -91,10 +88,8 @@ describe("account selection", () => {
}
});

afterEach(async () => {
afterEach(() => {
vi.restoreAllMocks();
setGlobalDispatcher(previousDispatcher);
await accountsApi.close();
});

function run(...flags: string[]) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/__tests__/commands/ai-run.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync, writeFileSync } from "node:fs";
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down Expand Up @@ -776,7 +776,7 @@ describe("cf ai run", () => {
});

it("validates and previews without running under --dry-run", async () => {
// No run handler: `onUnhandledRequest: "error"` proves nothing was
// No run handler: `onUnhandledFrame: "error"` proves nothing was
// sent to the inference endpoint.
server.use(schemaHandler());

Expand Down
27 changes: 9 additions & 18 deletions packages/cli/src/__tests__/commands/auth/profiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
mockConsoleMethods,
runInTempDir,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici";
import { afterAll, beforeAll, describe, expect, it, vi } from "vite-plus/test";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { server, setupMsw, TEST_BASE_URL } from "../../helpers/msw.js";
import { runCf } from "../../helpers/run-cf.js";

Expand All @@ -28,8 +27,6 @@ describe("cf auth profiles", () => {
runInTempDir();
setupMsw();
const std = mockConsoleMethods();
const mockAgent = new MockAgent();
const previousDispatcher = getGlobalDispatcher();
const store = createCfProfileStore({
logger: {
debug: () => {},
Expand All @@ -40,19 +37,13 @@ describe("cf auth profiles", () => {
},
});

beforeAll(() => {
mockAgent.disableNetConnect();
mockAgent
.get("https://dash.cloudflare.com")
.intercept({ path: "/oauth2/revoke", method: "POST" })
.reply(200, "")
.persist();
setGlobalDispatcher(mockAgent);
});

afterAll(async () => {
setGlobalDispatcher(previousDispatcher);
await mockAgent.close();
beforeEach(() => {
server.use(
http.post(
"https://dash.cloudflare.com/oauth2/revoke",
() => new HttpResponse("")
)
);
});

it("activates, lists, and deactivates a named profile", async () => {
Expand Down
71 changes: 28 additions & 43 deletions packages/cli/src/__tests__/commands/auth/whoami.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
mockConsoleMethods,
runInTempDir,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici";
import { http, HttpResponse } from "msw/http";
import { describe, expect, it } from "vite-plus/test";
import { server, setupMsw, TEST_BASE_URL } from "../../helpers/msw.js";
import { runCf } from "../../helpers/run-cf.js";
Expand All @@ -23,48 +22,34 @@ describe("cf auth whoami", () => {
)
);

const mockAgent = new MockAgent();
mockAgent.disableNetConnect();
const api = mockAgent.get("https://api.test");
api
.intercept({
path: "/client/v4/accounts?page=1",
method: "GET",
})
.reply(200, {
success: true,
result: [
{ id: "account-1", name: "Account One", settings: {} },
{ id: "account-2", name: "Account Two", settings: {} },
{ id: "account-3", name: "Account Three", settings: {} },
],
result_info: { page: 1, total_pages: 1 },
});
api
.intercept({
path: "/client/v4/memberships?page=1",
method: "GET",
})
.reply(200, {
success: true,
result: [
{ account: { id: "account-1", name: "Account One" } },
{ account: { id: "account-3", name: "Account Three" } },
],
result_info: { page: 1, total_pages: 1 },
});
const previousDispatcher = getGlobalDispatcher();
setGlobalDispatcher(mockAgent);
server.use(
http.get(`${TEST_BASE_URL}/accounts`, () =>
HttpResponse.json({
success: true,
result: [
{ id: "account-1", name: "Account One", settings: {} },
{ id: "account-2", name: "Account Two", settings: {} },
{ id: "account-3", name: "Account Three", settings: {} },
],
result_info: { page: 1, total_pages: 1 },
})
),
http.get(`${TEST_BASE_URL}/memberships`, () =>
HttpResponse.json({
success: true,
result: [
{ account: { id: "account-1", name: "Account One" } },
{ account: { id: "account-3", name: "Account Three" } },
],
result_info: { page: 1, total_pages: 1 },
})
)
);

try {
await runCf(["auth", "whoami"], {
CLOUDFLARE_API_TOKEN: "test-token",
CLOUDFLARE_API_BASE_URL: TEST_BASE_URL,
});
} finally {
setGlobalDispatcher(previousDispatcher);
await mockAgent.close();
}
await runCf(["auth", "whoami"], {
CLOUDFLARE_API_TOKEN: "test-token",
CLOUDFLARE_API_BASE_URL: TEST_BASE_URL,
});

expect(JSON.parse(std.out)).toMatchObject({
authenticated: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
mockConsoleMethods,
runInTempDir,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { USER_AGENT } from "../../version.js";
import { server, setupMsw, TEST_BASE_URL } from "../helpers/msw.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
mockConsoleMethods,
runInTempDir,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/commands/d1.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down Expand Up @@ -27,7 +27,7 @@ import { runCf } from "../helpers/run-cf.js";
*
* `runInTempDir` isolates HOME/XDG so a developer's stored OAuth token
* can't substitute for the fake env token under test. The MSW server
* uses `onUnhandledRequest: "error"`, so any request that escapes the
* uses `onUnhandledFrame: "error"`, so any request that escapes the
* mocks fails the test loudly rather than reaching the real API.
*/
describe("cf d1 (network)", () => {
Expand Down Expand Up @@ -171,7 +171,7 @@ describe("cf d1 (network)", () => {

it("delete without --force — aborts before any request", async () => {
// No DELETE handler registered: a request would trip the MSW
// onUnhandledRequest:"error" guard.
// onUnhandledFrame:"error" guard.
const { exitCode } = await runCf(["d1", "delete", "db-1"], ENV);

expect(exitCode).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
runInTempDir,
seed,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createMockDeployContext } from "../../helpers/mock-deploy-context.js";
import { createFetchResult, msw, setupMsw } from "../../helpers/msw.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
runInTempDir,
seed,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createMockDeployContext } from "../../helpers/mock-deploy-context.js";
import { createFetchResult, msw, setupMsw } from "../../helpers/msw.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/commands/deploy/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { onTestFinished } from "vite-plus/test";
import { makeExecutable, nodeScript } from "../../helpers/executable.js";
import { createFetchResult, msw } from "../../helpers/msw.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/commands/deploy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
runInTempDir,
seed,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createMockDeployContext } from "../../helpers/mock-deploy-context.js";
import { createFetchResult, msw, setupMsw } from "../../helpers/msw.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
runInTempDir,
seed,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createMockDeployContext } from "../../helpers/mock-deploy-context.js";
import { createFetchResult, msw, setupMsw } from "../../helpers/msw.js";
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/commands/kv.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down Expand Up @@ -27,7 +27,7 @@ import { runCf } from "../helpers/run-cf.js";
* rather than the JSON console.log path.
* - **Delete confirmation.** `kv namespaces delete` runs through
* `confirmDelete`; without `--force` in a non-TTY it aborts before
* any request (proven by the `onUnhandledRequest: "error"` guard).
* any request (proven by the `onUnhandledFrame: "error"` guard).
*
* `runInTempDir` isolates HOME/XDG so a developer's stored OAuth token
* can't substitute for the fake env token under test.
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("cf kv (network)", () => {

it("namespaces delete without --force — aborts before any request", async () => {
// No DELETE handler registered: if a request fired, the MSW
// onUnhandledRequest:"error" guard would fail the test.
// onUnhandledFrame:"error" guard would fail the test.
const { exitCode } = await runCf(
["kv", "namespaces", "delete", "ns-1"],
ENV
Expand Down Expand Up @@ -220,7 +220,7 @@ describe("cf kv (network)", () => {
});

it("--dry-run prints the planned request without hitting the network", async () => {
// No handler: a real request would trip onUnhandledRequest:"error".
// No handler: a real request would trip onUnhandledFrame:"error".
const { exitCode } = await runCf(
["kv", "namespaces", "create", "--title", "x", "--dry-run"],
ENV
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { runCf } from "../../helpers/run-cf.js";

/**
* `cf d1 migrations create`. Entirely local: MSW is armed with no handlers and
* `onUnhandledRequest: "error"`, so any outbound request would fail the
* `onUnhandledFrame: "error"`, so any outbound request would fail the
* test — which is the assertion that this command needs no credentials.
*/
describe("cf d1 migrations create", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import {
afterEach,
beforeEach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
runInTempDir,
seed,
} from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { http, HttpResponse } from "msw/http";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { createMockDeployContext } from "../helpers/mock-deploy-context.js";
import { createFetchResult, msw, setupMsw } from "../helpers/msw.js";
Expand Down
Loading
Loading