Skip to content

Commit 4a20621

Browse files
Implement verdaccio local npm registry for vitest-pool-workers tests (#7902)
* chore: configure eslint to ignore CommonJS config files (.cjs) as well as normal files (.js) * chore: suppress logs of cached builds and tests * chore: ensure wrangler can be published from Windows * chore: reduce noise on Windows tests using `run-in-tmp` * chore: ensure that NODE_EXTRA_CA_CERTS gets passed through turbo to jobs This was causing the C3 e2e tests to fail locally for people running behind WARP, since framework generators like Remix and Solid were trying to make Internet requests to get template files and crashing with self-signed certificates errors. * test: add new mock-npm-registry helper package * test: use new mock-npm-registry helper in vitest-pool-workers tests * test: use new mock-npm-registry helper in create-cloudflare e2e tests
1 parent 2636825 commit 4a20621

File tree

21 files changed

+2050
-118
lines changed

21 files changed

+2050
-118
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"unenv",
4949
"unrevoke",
5050
"Untriaged",
51+
"userconfig",
5152
"versionless",
5253
"wasmvalue",
5354
"weakmap",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { startMockNpmRegistry } from "@cloudflare/mock-npm-registry";
2+
3+
export default async () => startMockNpmRegistry("create-cloudflare");

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,15 @@ export const runC3 = async (
9797
promptHandlers: PromptHandler[] = [],
9898
logStream: Writable,
9999
) => {
100-
const cmd = ["node", "./dist/cli.js", ...argv];
101-
const proc = spawnWithLogging(cmd, { env: testEnv }, logStream);
100+
// We don't use the "test" package manager here (i.e. TEST_PM and TEST_PM_VERSION) because yarn 1.x doesn't actually provide a `dlx` version.
101+
// And in any case, this first step just installs a temp copy of create-cloudflare and executes it.
102+
// The point of `detectPackageManager()` is for delegating to framework tooling when generating a project correctly.
103+
const cmd = ["pnpx", "create-cloudflare", ...argv];
104+
const proc = spawnWithLogging(
105+
cmd,
106+
{ env: testEnv, cwd: tmpdir() },
107+
logStream,
108+
);
102109

103110
const onData = (data: string) => {
104111
handlePrompt(data);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ async function verifyTestScript(projectPath: string, logStream: Writable) {
344344
cwd: projectPath,
345345
env: {
346346
VITEST: undefined,
347+
// We need to fake that we are inside a CI
348+
// so that the `vitest` commands do not go into watch mode and hang.
349+
CI: "true",
347350
},
348351
},
349352
logStream,

packages/create-cloudflare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@clack/prompts": "^0.6.3",
4949
"@cloudflare/cli": "workspace:*",
5050
"@cloudflare/eslint-config-worker": "workspace:*",
51+
"@cloudflare/mock-npm-registry": "workspace:*",
5152
"@cloudflare/workers-tsconfig": "workspace:*",
5253
"@cloudflare/workers-types": "^4.20250129.0",
5354
"@iarna/toml": "^3.0.0",

packages/create-cloudflare/turbo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"E2E_RETRIES",
2525
"E2E_NO_DEPLOY",
2626
"E2E_EXPERIMENTAL",
27-
"TEST_PM"
27+
"TEST_PM",
28+
"TEST_PM_VERSION"
2829
],
2930
"dependsOn": ["build"],
3031
"outputs": [".e2e-logs", ".e2e-logs-experimental"]

packages/create-cloudflare/vitest-e2e.config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
root: ".",
1010
testTimeout: 1000 * 60 * 10, // 10 min for lengthy installs
1111
maxConcurrency: 3,
12+
globalSetup: ["e2e-tests/global-setup.ts"],
1213
setupFiles: ["e2e-tests/setup.ts", "dotenv/config"],
1314
reporters: ["json", "verbose", "hanging-process"],
1415
outputFile: {

packages/eslint-config-worker/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
"**/node_modules/**",
1010
"examples",
1111
"**/templates/**/*.*",
12-
".eslintrc.js",
12+
".eslintrc.*js",
1313
"**/dist/**",
1414
],
1515
parser: "@typescript-eslint/parser",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
root: true,
3+
extends: ["@cloudflare/eslint-config-worker"],
4+
ignorePatterns: ["/dist"],
5+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# mock-npm-registry
2+
3+
This internal package can be used in tests by othe packages to set up a mock local npm registry, publish locally built copies of the packages and their dependencies, then install those packages into test fixtures.
4+
5+
## Usage
6+
7+
A good example is the vitest-pool-workers e2e tests. See packages/vitest-pool-workers/test/global-setup.ts
8+
9+
```ts
10+
export default async function ({ provide }: GlobalSetupContext) {
11+
const stop = await startMockNpmRegistry("@cloudflare/vitest-pool-workers");
12+
13+
// Create temporary directory
14+
const projectPath = await createTestProject();
15+
execSync("pnpm install", { cwd: projectPath, stdio: "ignore" });
16+
17+
provide("tmpPoolInstallationPath", projectPath);
18+
```
19+
20+
Here you can see that in Vitest global setup file, we are calling `startMockNpmRegistry()`.
21+
We pass in the `@cloudflare/vitest-pool-workers` package name, which will ensure that this package
22+
(and all its local dependencies, such as Wrangler, Miniflare, etc) is built and published to the mock registry.
23+
24+
We then create a temporary test project that has a dependency on `@cloudflare/vitest-pool-workers`
25+
and then run `pnpm install`, which will install the locally published packages.
26+
27+
The path to this temporary test project is provided to tests.
28+
29+
## Debugging
30+
31+
If you are having problems with the mock npm registry, you can get additional debug logging by setting the `NODE_DEBUG` environment variable.
32+
33+
```env
34+
NODE_DEBUG=mock-npm-registry
35+
```

0 commit comments

Comments
 (0)