70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
86 lines
2.9 KiB
TypeScript
86 lines
2.9 KiB
TypeScript
import assert from "node:assert";
|
|
import childProcess from "node:child_process";
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { startMockNpmRegistry } from "@cloudflare/mock-npm-registry";
|
|
import { removeDir } from "@cloudflare/workers-utils";
|
|
import { version } from "../package.json";
|
|
import type { TestProject } from "vitest/node";
|
|
|
|
const repoRoot = path.resolve(__dirname, "../../..");
|
|
const packagesRoot = path.resolve(repoRoot, "packages");
|
|
|
|
// Using a global setup means we can modify tests without having to re-install
|
|
// packages into our temporary directory
|
|
export default async function ({ provide }: TestProject) {
|
|
const stop = await startMockNpmRegistry("@cloudflare/vitest-pool-workers");
|
|
|
|
// Create temporary directory
|
|
const projectPath = await createTestProject();
|
|
childProcess.execSync("pnpm install", { cwd: projectPath, stdio: "ignore" });
|
|
|
|
provide("tmpPoolInstallationPath", projectPath);
|
|
|
|
// Cleanup temporary directory on teardown
|
|
return async () => {
|
|
console.log("Closing down local npm registry");
|
|
await stop();
|
|
|
|
console.log("Cleaning up temporary directory...");
|
|
void removeDir(projectPath, { fireAndForget: true });
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create a temporary package that contains vitest-pool-workers and vitest.
|
|
*/
|
|
async function createTestProject() {
|
|
// Create temporary directory containing a space to avoid regressing on
|
|
// https://github.com/cloudflare/workers-sdk/issues/5268
|
|
const projectPath = await fs.realpath(
|
|
await fs.mkdtemp(path.join(os.tmpdir(), "vitest-pool-workers temp-"))
|
|
);
|
|
const packageJsonPath = path.join(projectPath, "package.json");
|
|
const vitestPeerDep = await getVitestPeerDep();
|
|
const packageJson = {
|
|
name: "vitest-pool-workers-e2e-tests",
|
|
private: true,
|
|
type: "module",
|
|
devDependencies: {
|
|
// Ensure we use the local version of vitest-pool-workers
|
|
"@cloudflare/vitest-pool-workers": version,
|
|
"@vitest/coverage-istanbul": vitestPeerDep,
|
|
vitest: vitestPeerDep,
|
|
},
|
|
};
|
|
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson));
|
|
// pnpm 10 blocks lifecycle scripts by default. The transitive deps
|
|
// (workerd, esbuild) need their postinstall to download platform binaries.
|
|
const workspaceYamlPath = path.join(projectPath, "pnpm-workspace.yaml");
|
|
await fs.writeFile(
|
|
workspaceYamlPath,
|
|
["allowBuilds:", " esbuild: true", " workerd: true", ""].join("\n")
|
|
);
|
|
return projectPath;
|
|
}
|
|
|
|
/**
|
|
* Get the version of `vitest` that is needed as a peer of vitest-pool-workers.
|
|
*/
|
|
async function getVitestPeerDep() {
|
|
const poolPackageJsonPath = path.join(
|
|
packagesRoot,
|
|
"vitest-pool-workers/package.json"
|
|
);
|
|
const poolPackageJson = JSON.parse(
|
|
await fs.readFile(poolPackageJsonPath, "utf8")
|
|
);
|
|
const poolVitestVersion = poolPackageJson.peerDependencies?.vitest;
|
|
assert(
|
|
typeof poolVitestVersion === "string",
|
|
"Expected to find `vitest` peer dependency version"
|
|
);
|
|
return poolVitestVersion;
|
|
}
|