Skip to content

Commit

Permalink
Implement verdaccio local npm registry for vitest-pool-workers tests (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
petebacondarwin authored Feb 4, 2025
1 parent 2636825 commit 4a20621
Show file tree
Hide file tree
Showing 21 changed files with 2,050 additions and 118 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"unenv",
"unrevoke",
"Untriaged",
"userconfig",
"versionless",
"wasmvalue",
"weakmap",
Expand Down
3 changes: 3 additions & 0 deletions packages/create-cloudflare/e2e-tests/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { startMockNpmRegistry } from "@cloudflare/mock-npm-registry";

export default async () => startMockNpmRegistry("create-cloudflare");
11 changes: 9 additions & 2 deletions packages/create-cloudflare/e2e-tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ export const runC3 = async (
promptHandlers: PromptHandler[] = [],
logStream: Writable,
) => {
const cmd = ["node", "./dist/cli.js", ...argv];
const proc = spawnWithLogging(cmd, { env: testEnv }, logStream);
// 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.
// And in any case, this first step just installs a temp copy of create-cloudflare and executes it.
// The point of `detectPackageManager()` is for delegating to framework tooling when generating a project correctly.
const cmd = ["pnpx", "create-cloudflare", ...argv];
const proc = spawnWithLogging(
cmd,
{ env: testEnv, cwd: tmpdir() },
logStream,
);

const onData = (data: string) => {
handlePrompt(data);
Expand Down
3 changes: 3 additions & 0 deletions packages/create-cloudflare/e2e-tests/workers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ async function verifyTestScript(projectPath: string, logStream: Writable) {
cwd: projectPath,
env: {
VITEST: undefined,
// We need to fake that we are inside a CI
// so that the `vitest` commands do not go into watch mode and hang.
CI: "true",
},
},
logStream,
Expand Down
1 change: 1 addition & 0 deletions packages/create-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@clack/prompts": "^0.6.3",
"@cloudflare/cli": "workspace:*",
"@cloudflare/eslint-config-worker": "workspace:*",
"@cloudflare/mock-npm-registry": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20250129.0",
"@iarna/toml": "^3.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/create-cloudflare/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"E2E_RETRIES",
"E2E_NO_DEPLOY",
"E2E_EXPERIMENTAL",
"TEST_PM"
"TEST_PM",
"TEST_PM_VERSION"
],
"dependsOn": ["build"],
"outputs": [".e2e-logs", ".e2e-logs-experimental"]
Expand Down
1 change: 1 addition & 0 deletions packages/create-cloudflare/vitest-e2e.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
root: ".",
testTimeout: 1000 * 60 * 10, // 10 min for lengthy installs
maxConcurrency: 3,
globalSetup: ["e2e-tests/global-setup.ts"],
setupFiles: ["e2e-tests/setup.ts", "dotenv/config"],
reporters: ["json", "verbose", "hanging-process"],
outputFile: {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
"**/node_modules/**",
"examples",
"**/templates/**/*.*",
".eslintrc.js",
".eslintrc.*js",
"**/dist/**",
],
parser: "@typescript-eslint/parser",
Expand Down
5 changes: 5 additions & 0 deletions packages/mock-npm-registry/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
root: true,
extends: ["@cloudflare/eslint-config-worker"],
ignorePatterns: ["/dist"],
};
35 changes: 35 additions & 0 deletions packages/mock-npm-registry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# mock-npm-registry

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.

## Usage

A good example is the vitest-pool-workers e2e tests. See packages/vitest-pool-workers/test/global-setup.ts

```ts
export default async function ({ provide }: GlobalSetupContext) {
const stop = await startMockNpmRegistry("@cloudflare/vitest-pool-workers");

// Create temporary directory
const projectPath = await createTestProject();
execSync("pnpm install", { cwd: projectPath, stdio: "ignore" });

provide("tmpPoolInstallationPath", projectPath);
```
Here you can see that in Vitest global setup file, we are calling `startMockNpmRegistry()`.
We pass in the `@cloudflare/vitest-pool-workers` package name, which will ensure that this package
(and all its local dependencies, such as Wrangler, Miniflare, etc) is built and published to the mock registry.
We then create a temporary test project that has a dependency on `@cloudflare/vitest-pool-workers`
and then run `pnpm install`, which will install the locally published packages.
The path to this temporary test project is provided to tests.
## Debugging
If you are having problems with the mock npm registry, you can get additional debug logging by setting the `NODE_DEBUG` environment variable.
```env
NODE_DEBUG=mock-npm-registry
```
27 changes: 27 additions & 0 deletions packages/mock-npm-registry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@cloudflare/mock-npm-registry",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./dist/index.js"
},
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"check:lint": "eslint . --max-warnings=0",
"check:types": "tsc"
},
"devDependencies": {
"@types/node": "catalog:default",
"@verdaccio/types": "^10.8.0",
"get-port": "^7.0.0",
"ts-dedent": "^2.2.0",
"typescript": "catalog:default",
"verdaccio": "^6.0.5"
},
"volta": {
"extends": "../../package.json"
}
}
Loading

0 comments on commit 4a20621

Please sign in to comment.