Skip to content

Commit 6c2e943

Browse files
authored
fix(create-cloudflare): bump vitest-pool-workers version on the templates (#7827)
* fix(create-cloudflare): bump vitest-pool-workers version on the templates * chore: verify the test script on hello-world template * chore: fix hello-world-js test * chore: add description for runner config * chore: test non-framework experimental templates * fixup! chore: test non-framework experimental templates * bootstraping projects with the experimental arg * do not skip experimental workers templates * experimental template does not infer category * project name can only be up to 58 chars * asset-only-template has no main file * make sure generated project name does not end with a dash
1 parent cccfe51 commit 6c2e943

File tree

8 files changed

+130
-32
lines changed

8 files changed

+130
-32
lines changed

.changeset/cuddly-radios-rule.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
fix: bump `vitest-pool-workers` version range in templates
6+
7+
This resolves [#7815](https://github.com/cloudflare/workers-sdk/issues/7815), where users encountered an error about missing `nodejs_compat` or `nodejs_compat_v2` compatibility flags when running Vitest on a fresh Hello World project created with `create-cloudflare`.

packages/create-cloudflare/e2e-tests/helpers.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,24 @@ export type RunnerConfig = {
7171
argv?: string[];
7272
quarantine?: boolean;
7373
timeout?: number;
74+
/**
75+
* Specifies whether to assert the response from the specified route after deployment.
76+
*/
7477
verifyDeploy: null | {
7578
route: string;
7679
expectedText: string;
7780
};
81+
/**
82+
* Specifies whether to run the preview script for the project and assert the response from the specified route.
83+
*/
7884
verifyPreview: null | {
7985
route: string;
8086
expectedText: string;
8187
};
88+
/**
89+
* Specifies whether to run the test script for the project and verify the exit code.
90+
*/
91+
verifyTest?: boolean;
8292
};
8393

8494
export const runC3 = async (
@@ -372,9 +382,18 @@ export const testProjectDir = (suite: string, test: string) => {
372382
const randomSuffix = crypto.randomBytes(4).toString("hex");
373383
const baseProjectName = `${C3_E2E_PREFIX}${randomSuffix}`;
374384

375-
const getName = () =>
385+
const getName = () => {
376386
// Worker project names cannot be longer than 58 characters
377-
`${baseProjectName}-${test.substring(0, 57 - baseProjectName.length)}`;
387+
const projectName = `${baseProjectName}-${test.substring(0, 57 - baseProjectName.length)}`;
388+
389+
// Project name cannot start/end with a dash
390+
if (projectName.endsWith("-")) {
391+
return projectName.slice(0, -1);
392+
}
393+
394+
return projectName;
395+
};
396+
378397
const getPath = () => path.join(tmpDirPath, getName());
379398
const clean = () => {
380399
try {

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

Lines changed: 97 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
runC3,
1515
spawnWithLogging,
1616
test,
17+
waitForExit,
1718
} from "./helpers";
1819
import type { RunnerConfig } from "./helpers";
1920
import type { Writable } from "stream";
@@ -30,12 +31,71 @@ type WorkerTestConfig = RunnerConfig & {
3031

3132
function getWorkerTests(opts: { experimental: boolean }): WorkerTestConfig[] {
3233
if (opts.experimental) {
33-
return [];
34+
return [
35+
{
36+
template: "hello-world-with-assets",
37+
variants: ["ts", "js"],
38+
verifyDeploy: {
39+
route: "/message",
40+
expectedText: "Hello, World!",
41+
},
42+
// There is no preview script
43+
verifyPreview: null,
44+
verifyTest: true,
45+
argv: ["--category", "hello-world"],
46+
},
47+
{
48+
template: "hello-world-with-assets",
49+
variants: ["python"],
50+
verifyDeploy: {
51+
route: "/message",
52+
expectedText: "Hello, World!",
53+
},
54+
// There is no preview script
55+
verifyPreview: null,
56+
argv: ["--category", "hello-world"],
57+
},
58+
{
59+
template: "hello-world-durable-object-with-assets",
60+
variants: ["ts", "js"],
61+
verifyDeploy: {
62+
route: "/",
63+
expectedText: "Hello, World!",
64+
},
65+
// There is no preview script
66+
verifyPreview: null,
67+
argv: ["--category", "hello-world"],
68+
},
69+
{
70+
template: "hello-world-assets-only",
71+
variants: [],
72+
verifyDeploy: {
73+
route: "/",
74+
expectedText: "Hello, World!",
75+
},
76+
// There is no preview script
77+
verifyPreview: null,
78+
argv: ["--category", "hello-world"],
79+
},
80+
];
3481
} else {
3582
return [
3683
{
3784
template: "hello-world",
38-
variants: ["TypeScript", "JavaScript", "Python"],
85+
variants: ["ts", "js"],
86+
verifyDeploy: {
87+
route: "/",
88+
expectedText: "Hello World!",
89+
},
90+
verifyPreview: {
91+
route: "/",
92+
expectedText: "Hello World!",
93+
},
94+
verifyTest: true,
95+
},
96+
{
97+
template: "hello-world",
98+
variants: ["python"],
3999
verifyDeploy: {
40100
route: "/",
41101
expectedText: "Hello World!",
@@ -47,7 +107,7 @@ function getWorkerTests(opts: { experimental: boolean }): WorkerTestConfig[] {
47107
},
48108
{
49109
template: "common",
50-
variants: ["TypeScript", "JavaScript"],
110+
variants: ["ts", "js"],
51111
verifyDeploy: {
52112
route: "/",
53113
expectedText: "Try making requests to:",
@@ -59,14 +119,14 @@ function getWorkerTests(opts: { experimental: boolean }): WorkerTestConfig[] {
59119
},
60120
{
61121
template: "queues",
62-
variants: ["TypeScript", "JavaScript"],
122+
variants: ["ts", "js"],
63123
// Skipped for now, since C3 does not yet support resource creation
64124
verifyDeploy: null,
65125
verifyPreview: null,
66126
},
67127
{
68128
template: "scheduled",
69-
variants: ["TypeScript", "JavaScript"],
129+
variants: ["ts", "js"],
70130
// Skipped for now, since it's not possible to test scheduled events on deployed Workers
71131
verifyDeploy: null,
72132
verifyPreview: null,
@@ -92,8 +152,7 @@ const workerTests = getWorkerTests({ experimental });
92152

93153
describe
94154
.skipIf(
95-
experimental || // skip until we add tests for experimental workers templates
96-
getFrameworkToTest({ experimental }) ||
155+
getFrameworkToTest({ experimental }) ||
97156
isQuarantineMode() ||
98157
process.platform === "win32",
99158
)
@@ -109,15 +168,7 @@ describe
109168
return {
110169
...testConfig,
111170
name: `${testConfig.name ?? testConfig.template}-${variant.toLowerCase()}`,
112-
promptHandlers: [
113-
{
114-
matcher: /Which language do you want to use\?/,
115-
input: {
116-
type: "select",
117-
target: variant,
118-
},
119-
},
120-
],
171+
argv: (testConfig.argv ?? []).concat("--lang", variant),
121172
};
122173
})
123174
: [testConfig],
@@ -137,9 +188,6 @@ describe
137188
// Relevant project files should have been created
138189
expect(project.path).toExist();
139190

140-
const gitignorePath = join(project.path, ".gitignore");
141-
expect(gitignorePath).toExist();
142-
143191
const pkgJsonPath = join(project.path, "package.json");
144192
expect(pkgJsonPath).toExist();
145193

@@ -151,22 +199,30 @@ describe
151199

152200
try {
153201
expect(jsonPath).toExist();
154-
const config = readJSON(jsonPath) as { main: string };
155-
expect(join(project.path, config.main)).toExist();
156-
} catch {
202+
const config = readJSON(jsonPath) as { main?: string };
203+
if (config.main) {
204+
expect(join(project.path, config.main)).toExist();
205+
}
206+
} catch (e) {
157207
expect(tomlPath).toExist();
158-
const config = readToml(tomlPath) as { main: string };
159-
expect(join(project.path, config.main)).toExist();
208+
const config = readToml(tomlPath) as { main?: string };
209+
if (config.main) {
210+
expect(join(project.path, config.main)).toExist();
211+
}
160212
}
161213

162-
const { verifyDeploy } = testConfig;
214+
const { verifyDeploy, verifyTest } = testConfig;
163215
if (verifyDeploy) {
164216
if (deployedUrl) {
165217
await verifyDeployment(deployedUrl, verifyDeploy);
166218
} else {
167219
await verifyLocalDev(testConfig, project.path, logStream);
168220
}
169221
}
222+
223+
if (verifyTest) {
224+
await verifyTestScript(project.path, logStream);
225+
}
170226
} finally {
171227
await deleteWorker(project.name);
172228
}
@@ -185,6 +241,7 @@ const runCli = async (
185241
projectPath,
186242
"--type",
187243
template,
244+
...(experimental ? ["--experimental"] : []),
188245
"--no-open",
189246
"--no-git",
190247
NO_DEPLOY ? "--no-deploy" : "--deploy",
@@ -279,3 +336,18 @@ const verifyLocalDev = async (
279336
await sleep(1000);
280337
}
281338
};
339+
340+
async function verifyTestScript(projectPath: string, logStream: Writable) {
341+
const proc = spawnWithLogging(
342+
[pm, "run", "test"],
343+
{
344+
cwd: projectPath,
345+
env: {
346+
VITEST: undefined,
347+
},
348+
},
349+
logStream,
350+
);
351+
352+
return await waitForExit(proc);
353+
}

packages/create-cloudflare/templates-experimental/hello-world-with-assets/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test": "vitest"
1010
},
1111
"devDependencies": {
12-
"@cloudflare/vitest-pool-workers": "^0.5.2",
12+
"@cloudflare/vitest-pool-workers": "^0.6.4",
1313
"wrangler": "^3.101.0",
1414
"vitest": "2.1.8"
1515
}

packages/create-cloudflare/templates-experimental/hello-world-with-assets/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"cf-typegen": "wrangler types"
1111
},
1212
"devDependencies": {
13-
"@cloudflare/vitest-pool-workers": "^0.5.2",
13+
"@cloudflare/vitest-pool-workers": "^0.6.4",
1414
"typescript": "^5.5.2",
1515
"vitest": "2.1.8",
1616
"wrangler": "^3.101.0"

packages/create-cloudflare/templates/hello-world/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test": "vitest"
1010
},
1111
"devDependencies": {
12-
"@cloudflare/vitest-pool-workers": "^0.5.2",
12+
"@cloudflare/vitest-pool-workers": "^0.6.4",
1313
"wrangler": "^3.101.0",
1414
"vitest": "2.1.8"
1515
}

packages/create-cloudflare/templates/hello-world/js/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Hello World worker', () => {
1414
});
1515

1616
it('responds with Hello World! (integration style)', async () => {
17-
const response = await SELF.fetch(request, env, ctx);
17+
const response = await SELF.fetch('http://example.com');
1818
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
1919
});
2020
});

packages/create-cloudflare/templates/hello-world/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"cf-typegen": "wrangler types"
1111
},
1212
"devDependencies": {
13-
"@cloudflare/vitest-pool-workers": "^0.5.2",
13+
"@cloudflare/vitest-pool-workers": "^0.6.4",
1414
"typescript": "^5.5.2",
1515
"vitest": "2.1.8",
1616
"wrangler": "^3.101.0"

0 commit comments

Comments
 (0)