Skip to content

Commit 7f97ff8

Browse files
committed
update tests
1 parent 1bbd93a commit 7f97ff8

File tree

3 files changed

+89
-51
lines changed

3 files changed

+89
-51
lines changed

packages/create-cloudflare/e2e-tests/frameworks.test.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -361,48 +361,32 @@ const verifyBuildCfTypesScript = async (
361361
return;
362362
}
363363

364-
const { outputFile, envInterfaceName } = verifyBuildCfTypes;
365-
366-
const outputFileContentPre = readFile(join(projectPath, outputFile));
367-
const outputFileContentPreLines = outputFileContentPre.split("\n");
368-
369-
// the file contains the "Generated by Wrangler" comment without a timestamp
370-
expect(outputFileContentPreLines).toContain("// Generated by Wrangler");
371-
372-
// the file contains the env interface
373-
// the file still contains the env interface
374-
const hasEnvInterfacePre = outputFileContentPreLines.some(
375-
(line) =>
376-
// old type gen - some framework templates pin older versions of wrangler
377-
line === `interface ${envInterfaceName} {` ||
378-
// new after importable env change
379-
line === `interface ${envInterfaceName} extends Cloudflare.Env {}`,
380-
);
381-
expect(hasEnvInterfacePre).toBe(true);
382-
383364
// Run the `cf-typegen` script to generate types for bindings in fixture
384365
const buildTypesProc = spawnWithLogging(
385366
[pm, "run", "cf-typegen"],
386367
{ cwd: projectPath },
387368
logStream,
388369
);
370+
389371
await waitForExit(buildTypesProc);
390372

391-
const outputFileContentPost = readFile(join(projectPath, outputFile));
392-
const outputFileContentPostLines = outputFileContentPost.split("\n");
373+
const { outputFile, envInterfaceName } = verifyBuildCfTypes;
393374

394-
// the file doesn't contain the "Generated by Wrangler" comment anymore
395-
expect(outputFileContentPostLines).not.toContain("// Generated by Wrangler");
375+
const outputFileContent = readFile(join(projectPath, outputFile)).split("\n");
396376

397-
// the file still contains the env interface
398-
const hasEnvInterfacePost = outputFileContentPostLines.some(
377+
// the file contains the env interface
378+
const hasEnvInterfacePre = outputFileContent.some(
399379
(line) =>
400380
// old type gen - some framework templates pin older versions of wrangler
401381
line === `interface ${envInterfaceName} {` ||
402382
// new after importable env change
403383
line === `interface ${envInterfaceName} extends Cloudflare.Env {}`,
404384
);
405-
expect(hasEnvInterfacePost).toBe(true);
385+
expect(hasEnvInterfacePre).toBe(true);
386+
387+
expect(outputFileContent[1]).match(
388+
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/,
389+
);
406390
};
407391

408392
const verifyBuildScript = async (

packages/create-cloudflare/e2e-tests/frameworks/framework-test-config.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export default function getFrameworkTestConfig(pm: string) {
4040
route: "/test",
4141
expectedText: "C3_TEST",
4242
},
43+
verifyBuildCfTypes: {
44+
outputFile: "worker-configuration.d.ts",
45+
envInterfaceName: "Env",
46+
},
4347
flags: [
4448
"--skip-houston",
4549
"--no-install",
@@ -68,6 +72,10 @@ export default function getFrameworkTestConfig(pm: string) {
6872
route: "/test",
6973
expectedText: "C3_TEST",
7074
},
75+
verifyBuildCfTypes: {
76+
outputFile: "worker-configuration.d.ts",
77+
envInterfaceName: "Env",
78+
},
7179
flags: [
7280
"--skip-houston",
7381
"--no-install",
@@ -184,6 +192,10 @@ export default function getFrameworkTestConfig(pm: string) {
184192
route: "/",
185193
expectedText: "Congratulations! Your app is running.",
186194
},
195+
verifyBuildCfTypes: {
196+
outputFile: "worker-configuration.d.ts",
197+
envInterfaceName: "Env",
198+
},
187199
flags: ["--style", "sass"],
188200
},
189201
"gatsby:pages": {
@@ -238,6 +250,10 @@ export default function getFrameworkTestConfig(pm: string) {
238250
route: "/",
239251
expectedText: "Hello!",
240252
},
253+
verifyBuildCfTypes: {
254+
outputFile: "worker-configuration.d.ts",
255+
envInterfaceName: "CloudflareBindings",
256+
},
241257
promptHandlers: [
242258
{
243259
matcher: /Do you want to install project dependencies\?/,
@@ -257,6 +273,10 @@ export default function getFrameworkTestConfig(pm: string) {
257273
route: "/message",
258274
expectedText: "Hello Hono!",
259275
},
276+
verifyBuildCfTypes: {
277+
outputFile: "worker-configuration.d.ts",
278+
envInterfaceName: "CloudflareBindings",
279+
},
260280
promptHandlers: [
261281
{
262282
matcher: /Do you want to install project dependencies\?/,
@@ -283,10 +303,6 @@ export default function getFrameworkTestConfig(pm: string) {
283303
route: "/",
284304
expectedText: "Welcome to Qwik",
285305
},
286-
verifyBuildCfTypes: {
287-
outputFile: "worker-configuration.d.ts",
288-
envInterfaceName: "Env",
289-
},
290306
},
291307
"qwik:workers": {
292308
argv: ["--platform", "workers"],
@@ -308,10 +324,6 @@ export default function getFrameworkTestConfig(pm: string) {
308324
route: "/",
309325
expectedText: "Welcome to Qwik",
310326
},
311-
verifyBuildCfTypes: {
312-
outputFile: "worker-configuration.d.ts",
313-
envInterfaceName: "Env",
314-
},
315327
},
316328
"remix:pages": {
317329
argv: ["--platform", "pages"],
@@ -327,10 +339,6 @@ export default function getFrameworkTestConfig(pm: string) {
327339
route: "/test",
328340
expectedText: "C3_TEST",
329341
},
330-
verifyBuildCfTypes: {
331-
outputFile: "worker-configuration.d.ts",
332-
envInterfaceName: "Env",
333-
},
334342
verifyBuild: {
335343
outputDir: "./build/client",
336344
script: "build",
@@ -353,10 +361,6 @@ export default function getFrameworkTestConfig(pm: string) {
353361
route: "/test",
354362
expectedText: "C3_TEST",
355363
},
356-
verifyBuildCfTypes: {
357-
outputFile: "worker-configuration.d.ts",
358-
envInterfaceName: "Env",
359-
},
360364
flags: ["--typescript", "--no-install", "--no-git-init"],
361365
},
362366
"next:pages": {
@@ -506,6 +510,10 @@ export default function getFrameworkTestConfig(pm: string) {
506510
route: "/",
507511
expectedText: "Vite + React",
508512
},
513+
verifyBuildCfTypes: {
514+
outputFile: "worker-configuration.d.ts",
515+
envInterfaceName: "Env",
516+
},
509517
},
510518
"react:workers": {
511519
argv: ["--platform", "workers"],
@@ -534,6 +542,10 @@ export default function getFrameworkTestConfig(pm: string) {
534542
// not actually running the client side JS.
535543
expectedText: "Vite + React + TS",
536544
},
545+
verifyBuildCfTypes: {
546+
outputFile: "worker-configuration.d.ts",
547+
envInterfaceName: "Env",
548+
},
537549
},
538550
solid: {
539551
promptHandlers: [
@@ -558,6 +570,10 @@ export default function getFrameworkTestConfig(pm: string) {
558570
route: "/",
559571
expectedText: "Hello world",
560572
},
573+
verifyBuildCfTypes: {
574+
outputFile: "worker-configuration.d.ts",
575+
envInterfaceName: "Env",
576+
},
561577
},
562578
"svelte:pages": {
563579
argv: ["--platform", "pages"],
@@ -568,7 +584,7 @@ export default function getFrameworkTestConfig(pm: string) {
568584
},
569585
{
570586
matcher: /Add type checking with Typescript/,
571-
input: [keys.down, keys.enter],
587+
input: [keys.enter],
572588
},
573589
{
574590
matcher: /What would you like to add to your project/,
@@ -597,6 +613,10 @@ export default function getFrameworkTestConfig(pm: string) {
597613
route: "/test",
598614
expectedText: "C3_TEST",
599615
},
616+
verifyBuildCfTypes: {
617+
outputFile: "./src/worker-configuration.d.ts",
618+
envInterfaceName: "Env",
619+
},
600620
},
601621
"svelte:workers": {
602622
argv: ["--platform", "workers"],
@@ -607,7 +627,7 @@ export default function getFrameworkTestConfig(pm: string) {
607627
},
608628
{
609629
matcher: /Add type checking with Typescript/,
610-
input: [keys.down, keys.enter],
630+
input: [keys.enter],
611631
},
612632
{
613633
matcher: /What would you like to add to your project/,
@@ -630,6 +650,10 @@ export default function getFrameworkTestConfig(pm: string) {
630650
route: "/test",
631651
expectedText: "C3_TEST",
632652
},
653+
verifyBuildCfTypes: {
654+
outputFile: "./src/worker-configuration.d.ts",
655+
envInterfaceName: "Env",
656+
},
633657
},
634658
"vue:pages": {
635659
argv: ["--platform", "pages"],
@@ -643,6 +667,10 @@ export default function getFrameworkTestConfig(pm: string) {
643667
route: "/",
644668
expectedText: "Vite App",
645669
},
670+
verifyBuildCfTypes: {
671+
outputFile: "worker-configuration.d.ts",
672+
envInterfaceName: "Env",
673+
},
646674
flags: ["--ts"],
647675
},
648676
"vue:workers": {
@@ -658,6 +686,10 @@ export default function getFrameworkTestConfig(pm: string) {
658686
route: "/",
659687
expectedText: "Vite App",
660688
},
689+
verifyBuildCfTypes: {
690+
outputFile: "worker-configuration.d.ts",
691+
envInterfaceName: "Env",
692+
},
661693
},
662694
};
663695
}

packages/create-cloudflare/src/__tests__/workers.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mockSpinner } from "helpers/__tests__/mocks";
33
import { getLatestTypesEntrypoint } from "helpers/compatDate";
44
import { readFile, writeFile } from "helpers/files";
55
import { beforeEach, describe, expect, test, vi } from "vitest";
6-
import { addWorkersTypesToTsConfig } from "../workers";
6+
import { updateTsConfig } from "../workers";
77
import { createTestContext } from "./helpers";
88
import type { C3Context } from "types";
99

@@ -18,12 +18,13 @@ beforeEach(() => {
1818

1919
const mockCompatDate = "2024-01-17";
2020

21-
describe("addWorkersTypesToTsConfig", () => {
21+
describe("updateTsConfig", () => {
2222
let ctx: C3Context;
2323

2424
beforeEach(() => {
2525
ctx = createTestContext();
2626
ctx.args.ts = true;
27+
ctx.template.installWorkersTypes = false;
2728

2829
vi.mocked(existsSync).mockImplementation(() => true);
2930
vi.mocked(getLatestTypesEntrypoint).mockReturnValue(mockCompatDate);
@@ -34,8 +35,9 @@ describe("addWorkersTypesToTsConfig", () => {
3435
);
3536
});
3637

37-
test("happy path", async () => {
38-
await addWorkersTypesToTsConfig(ctx);
38+
test("installing workers types", async () => {
39+
ctx.template.installWorkersTypes = true;
40+
await updateTsConfig(ctx);
3941

4042
expect(writeFile).toHaveBeenCalled();
4143

@@ -46,26 +48,46 @@ describe("addWorkersTypesToTsConfig", () => {
4648

4749
test("tsconfig.json not found", async () => {
4850
vi.mocked(existsSync).mockImplementation(() => false);
49-
await addWorkersTypesToTsConfig(ctx);
51+
await updateTsConfig(ctx);
5052
expect(writeFile).not.toHaveBeenCalled();
5153
});
5254

5355
test("latest entrypoint not found", async () => {
56+
ctx.template.installWorkersTypes = true;
5457
vi.mocked(getLatestTypesEntrypoint).mockReturnValue(null);
55-
await addWorkersTypesToTsConfig(ctx);
58+
await updateTsConfig(ctx);
5659

5760
expect(writeFile).not.toHaveBeenCalled();
5861
});
5962

6063
test("don't clobber existing entrypoints", async () => {
64+
ctx.template.installWorkersTypes = true;
6165
vi.mocked(readFile).mockImplementation(
6266
() =>
6367
`{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`,
6468
);
65-
await addWorkersTypesToTsConfig(ctx);
69+
await updateTsConfig(ctx);
6670

6771
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(
6872
`"@cloudflare/workers-types/2021-03-20"`,
6973
);
7074
});
75+
76+
test("will remove workers-types when generating types", async () => {
77+
vi.mocked(readFile).mockImplementation(
78+
() =>
79+
`{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`,
80+
);
81+
await updateTsConfig(ctx);
82+
expect(vi.mocked(writeFile).mock.calls[0][1]).not.toContain(
83+
`"@cloudflare/workers-types/2021-03-20"`,
84+
);
85+
});
86+
87+
test("will add generated types file", async () => {
88+
await updateTsConfig(ctx);
89+
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(
90+
`./worker-configuration.d.ts`,
91+
);
92+
});
7193
});

0 commit comments

Comments
 (0)