Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jul 10, 2024
1 parent 57664a7 commit 6c26770
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 87 deletions.
9 changes: 8 additions & 1 deletion packages/wrangler/src/__tests__/helpers/run-wrangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import { normalizeSlashes, stripTimings } from "./mock-console";
/**
* A helper to 'run' wrangler commands for tests.
*/
export async function runWrangler(cmd = "") {
export async function runWrangler(
cmd = "",
env: Record<string, string | undefined> = {}
) {
const originalEnv = process.env;
try {
process.env = { ...originalEnv, ...env };
const argv = shellquote.parse(cmd);
await main(argv);
} catch (err) {
if (err instanceof Error) {
err.message = normalizeSlashes(stripTimings(err.message));
}
throw err;
} finally {
process.env = originalEnv;
}
}
13 changes: 8 additions & 5 deletions packages/wrangler/src/__tests__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ describe("logout", () => {
const std = mockConsoleMethods();

it("should exit with a message stating the user is not logged in", async () => {
await runWrangler("logout");
await runWrangler("logout", { CLOUDFLARE_API_TOKEN: undefined });
expect(std.out).toMatchInlineSnapshot(`"Not logged in, exiting..."`);
});

it("should exit with a message stating the user logged in via API token", async () => {
await runWrangler("logout", { CLOUDFLARE_API_TOKEN: "DUMMY_TOKEN" });
expect(std.out).toMatchInlineSnapshot(
`"You are logged in with an API Token. Unset the CLOUDFLARE_API_TOKEN in the environment to log out."`
);
Expand Down Expand Up @@ -39,11 +44,9 @@ describe("logout", () => {

expect(fs.existsSync(config)).toBeTruthy();

await runWrangler("logout");
await runWrangler("logout", { CLOUDFLARE_API_TOKEN: undefined });

expect(std.out).toMatchInlineSnapshot(
`"You are logged in with an API Token. Unset the CLOUDFLARE_API_TOKEN in the environment to log out."`
);
expect(std.out).toMatchInlineSnapshot(`"Successfully logged out."`);
expect(fs.existsSync(config)).toBeFalsy();
expect(counter).toBe(1);
});
Expand Down
90 changes: 18 additions & 72 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,9 @@ describe("metrics", () => {
await dispatcher.identify({ a: 1, b: 2 });

expect(request.count).toBe(1);
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"
`);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand All @@ -100,18 +91,9 @@ describe("metrics", () => {
await flushPromises();

expect(requests.count).toBe(0);
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."
`);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand All @@ -128,18 +110,9 @@ describe("metrics", () => {
await dispatcher.identify({ a: 1, b: 2 });
await flushPromises();
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: request to https://sparrow.cloudflare.com/api/v1/identify failed, reason: BAD REQUEST"
`);
"Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: Failed to fetch"
`);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -180,18 +153,9 @@ describe("metrics", () => {
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });

expect(requests.count).toBe(1);
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"
`);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand All @@ -208,18 +172,9 @@ describe("metrics", () => {
await flushPromises();

expect(requests.count).toBe(0);
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."
`);
expect(std.debug).toMatchInlineSnapshot(
`"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand All @@ -235,18 +190,9 @@ describe("metrics", () => {
await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
await flushPromises();
expect(std.debug).toMatchInlineSnapshot(`
"-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/user
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API REQUEST
-- START CF API RESPONSE: OK 200
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
-- END CF API RESPONSE
Saving to cache: {\\"userId\\":\\"MOCK_USER_ID\\"}
Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: request to https://sparrow.cloudflare.com/api/v1/event failed, reason: BAD REQUEST"
`);
"Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
Metrics dispatcher: Failed to send request: Failed to fetch"
`);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
Expand Down
13 changes: 5 additions & 8 deletions packages/wrangler/src/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ describe("User", () => {
mockExchangeRefreshTokenForAccessToken({ respondWith: "refreshError" });

// Handles the requireAuth error throw from failed login that is unhandled due to directly calling it here
await expect(requireAuth({} as Config)).rejects
.toThrowErrorMatchingInlineSnapshot(`
"More than one account available but unable to select one in non-interactive mode.
Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
Available accounts are (\`<name>\`: \`<account_id>\`):
\`My Personal Account\`: \`account-id-1\`
\`Enterprise Account\`: \`account-id-2\`"
`);
await expect(
requireAuth({} as Config)
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.]`
);
});

it("should confirm no error message when refresh is successful", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ describe("getUserInfo()", () => {
writeAuthConfigFile({ api_token: "API_TOKEN" });
await getUserInfo();

expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`
"▲ [WARNING] It looks like you have used Wrangler v1's \`config\` command to login with an API token.
This is no longer supported in the current version of Wrangler.
If you wish to authenticate via an API token then please set the \`CLOUDFLARE_API_TOKEN\`
environment variable.
"
`);
});
});

0 comments on commit 6c26770

Please sign in to comment.