Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/vitest-pool-workers-vite-plus-compat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@cloudflare/vitest-pool-workers": patch
---

Support `@voidzero-dev/vite-plus-test` as an alternative to `vitest`

Users running tests via [Vite+](https://github.com/voidzero-dev/vite-plus) (`@voidzero-dev/vite-plus-test`) with the [recommended pnpm overrides](https://github.com/voidzero-dev/vite-plus/blob/main/packages/test/BUNDLING.md) no longer hit spurious version warnings or `Disallowed operation called within global scope` errors.
17 changes: 16 additions & 1 deletion packages/vitest-pool-workers/src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,10 @@ export async function connectToMiniflareSocket(
}

interface PackageJson {
name?: string;
version?: string;
peerDependencies?: Record<string, string | undefined>;
bundledVersions?: Record<string, string | undefined>;
}
function getPackageJson(dirPath: string): PackageJson | undefined {
while (true) {
Expand All @@ -727,6 +729,15 @@ function getPackageJson(dirPath: string): PackageJson | undefined {
}
}

/**
* Extract the upstream vitest version from an alternative distribution's
* package.json. Distributions like `@voidzero-dev/vite-plus-test` declare
* the bundled vitest version in `bundledVersions.vitest`.
*/
function getUpstreamVitestVersion(pkgJson: PackageJson): string | undefined {
return pkgJson.bundledVersions?.vitest;
Comment thread
penalosa marked this conversation as resolved.
}

export function assertCompatibleVitestVersion(ctx: Vitest) {
// Some package managers don't enforce `peerDependencies` requirements,
// so add a runtime sanity check to ensure things don't break in strange ways.
Expand All @@ -742,11 +753,15 @@ export function assertCompatibleVitestVersion(ctx: Vitest) {
);

const expectedVitestVersion = poolPkgJson.peerDependencies?.vitest;
const actualVitestVersion = vitestPkgJson.version;
assert(
expectedVitestVersion !== undefined,
"Expected to find `@cloudflare/vitest-pool-workers`'s `vitest` version constraint"
);

const actualVitestVersion =
vitestPkgJson.name === "vitest"
? vitestPkgJson.version
: (getUpstreamVitestVersion(vitestPkgJson) ?? vitestPkgJson.version);
assert(
actualVitestVersion !== undefined,
"Expected to find `vitest`'s version"
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest-pool-workers/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ const monkeypatchedSetTimeout = (...args: Parameters<typeof setTimeout>) => {
const callerFileName = getCallerFileName(monkeypatchedSetTimeout);
const fromVitest =
/\/node_modules\/(\.pnpm\/|\.store\/)?vitest/.test(callerFileName ?? "") ||
/\/packages\/vitest\/dist/.test(callerFileName ?? "");
/\/packages\/vitest\/dist/.test(callerFileName ?? "") ||
/\/node_modules\/(\.pnpm\/|\.store\/)?@voidzero-dev[+/]vite-plus-test/.test(
callerFileName ?? ""
);

// If this `setTimeout()` isn't from Vitest, or has a non-zero delay,
// just call the original function
Expand Down
Loading