Skip to content

Commit

Permalink
test: handle normalization of test output automatically
Browse files Browse the repository at this point in the history
Adds the `normalizeSlashes()` and `stripTimings()` helpers automatically to output and errors,
so that the test author doesn't have to remember to add them.
  • Loading branch information
petebacondarwin committed Jan 31, 2022
1 parent 73e0c1b commit 589828d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 245 deletions.
8 changes: 7 additions & 1 deletion packages/wrangler/src/__tests__/helpers/run-wrangler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { main } from "../../index";
import { normalizeSlashes, stripTimings } from "./mock-console";

/**
* A helper to 'run' wrangler commands for tests.
*/
export async function runWrangler(cmd?: string) {
await main(cmd?.split(" ") ?? []);
try {
await main(cmd?.split(" ") ?? []);
} catch (e) {
e.message = normalizeSlashes(stripTimings(e.message));
throw e;
}
}
68 changes: 25 additions & 43 deletions packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ describe("wrangler", () => {

describe("invalid command", () => {
it("should display an error", async () => {
let err: Error | undefined;
try {
await runWrangler("invalid-command");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`Unknown command: invalid-command.`);
}
await expect(
runWrangler("invalid-command")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unknown command: invalid-command."`
);

expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -370,50 +367,35 @@ describe("wrangler", () => {
});

it("should error if `--type` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});

it("should error if `--type javascript` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type javascript");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type javascript")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});

it("should error if `--type rust` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type rust");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type rust")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});

it("should error if `--type webpack` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type webpack");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(
`The --type option is no longer supported.
If you wish to use webpack then you will need to create a custom build.`
);
}
await expect(runWrangler("init --type webpack")).rejects
.toThrowErrorMatchingInlineSnapshot(`
"The --type option is no longer supported.
If you wish to use webpack then you will need to create a custom build."
`);
});
});
});
Loading

0 comments on commit 589828d

Please sign in to comment.