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
49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import util from "node:util";
|
|
import { startMockNpmRegistry } from "@cloudflare/mock-npm-registry";
|
|
import { removeDir } from "@cloudflare/workers-utils";
|
|
import type { TestProject } from "vitest/node";
|
|
|
|
const debuglog = util.debuglog("vite-plugin:test");
|
|
|
|
declare module "vitest" {
|
|
export interface ProvidedContext {
|
|
root: string;
|
|
}
|
|
}
|
|
|
|
// 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 stopMockNpmRegistry = await startMockNpmRegistry(
|
|
"@cloudflare/vite-plugin"
|
|
);
|
|
|
|
// Create temporary directory to host projects used for testing.
|
|
// On Windows GitHub Actions runners, `os.tmpdir()` returns a path containing
|
|
// the 8.3 short name "RUNNER~1". Vite 8.0.16 (and the backports to v6/v7)
|
|
// rejects paths containing "~" or ":" as a security fix
|
|
// (https://github.com/vitejs/vite/pull/22572, GHSA-fx2h-pf6j-xcff), so we
|
|
// resolve the temp dir to its real long-form path first.
|
|
const tmpdir = await fs.realpath(os.tmpdir());
|
|
const root = await fs.mkdtemp(path.join(tmpdir, "vite-plugin-"));
|
|
debuglog("Created temporary directory at " + root);
|
|
|
|
// The type of the provided `root` is defined in the `ProvidedContent` type above.
|
|
provide("root", root);
|
|
|
|
// Cleanup temporary directory on teardown
|
|
return async () => {
|
|
await stopMockNpmRegistry();
|
|
|
|
if (process.env.CLOUDFLARE_VITE_E2E_KEEP_TEMP_DIRS) {
|
|
debuglog("Temporary directory left in-place at " + root);
|
|
} else {
|
|
debuglog("Cleaning up temporary directory...");
|
|
await removeDir(root);
|
|
}
|
|
};
|
|
}
|