Skip to content

Commit

Permalink
fix and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Feb 20, 2025
1 parent 41fcbd9 commit 9a873ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
50 changes: 32 additions & 18 deletions packages/wrangler/e2e/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,43 @@ describe("types", () => {
"FAKE RUNTIME",
].join("\n")
);
console.log(
[
file[0],
file[1],
"FAKE ENV",
"// Begin runtime types",
"FAKE RUNTIME",
].join("\n")
);

await helper.run(`wrangler types`);

const file2 = (await readFile(typesPath)).toString();

expect(file2).toMatchInlineSnapshot(`
"// Generated by Wrangler by running \`wrangler types\` (hash: e82ba4d7b995dd9ca6fb0332d81f889b)
// Runtime types generated with [email protected] 2023-01-01 no_global_navigator,nodejs_compat
// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type
interface Env {
}
// regenerates env types
expect(file2).toContain("interface Env {");
// uses cached runtime types
expect(file2).toContain("// Begin runtime types");
expect(file2).toContain("FAKE RUNTIME");
});

// Begin runtime types
FAKE RUNTIME"
`);
it("should prompt you to update types if they've been changed", async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed(seed);
await helper.run(`wrangler types`);
seed["wrangler.toml"] = dedent`
name = "test-worker"
main = "src/index.ts"
compatibility_date = "2023-01-01"
compatibility_flags = ["nodejs_compat", "no_global_navigator"]
[vars]
BEEP = "BOOP"
`;
await helper.seed(seed);
const worker = helper.runLongLived("wrangler dev");
await worker.readUntil(/ It looks like your types might be out of date./);
seed["wrangler.toml"] = dedent`
name = "test-worker"
main = "src/index.ts"
compatibility_date = "2023-01-01"
compatibility_flags = ["nodejs_compat"]
[vars]
BEEP = "BOOP"
ASDf = "ADSfadsf"
`;
await helper.seed(seed);
await worker.readUntil(/ It looks like your types might be out of date./);
});
});
4 changes: 3 additions & 1 deletion packages/wrangler/src/type-generation/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const checkTypesDiff = async (config: Config, entry: Entry) => {
}
let maybeExistingTypesFile: string[];
try {
// Note: this checks the default location only
maybeExistingTypesFile = readFileSync(
"./worker-configuration.d.ts",
"utf-8"
Expand Down Expand Up @@ -56,7 +57,8 @@ export const checkTypesDiff = async (config: Config, entry: Entry) => {
const envOutOfDate = existingEnvHeader && maybeExistingHash !== newHash;
const runtimeOutOfDate =
existingRuntimeHeader && existingRuntimeHeader !== newRuntimeHeader;
if (envOutOfDate && runtimeOutOfDate) {

if (envOutOfDate || runtimeOutOfDate) {
logger.log(
"❓ It looks like your types might be out of date. Have you updated your config file since last running `wrangler types`?"
);
Expand Down

0 comments on commit 9a873ab

Please sign in to comment.