Skip to content

Commit

Permalink
Support Vitest 3 (#7923)
Browse files Browse the repository at this point in the history
* Support Vitest 3

* no hangin timeout

* more changes

* faster tests?

* turbo

* update lockfile

* fix lockfile

* fix

* lint & windows

* fix lockfile

* lockfile

* fix lint

* Support v2 as well

* changeset

* lockfile
  • Loading branch information
penalosa authored Feb 14, 2025
1 parent 9f482ad commit aaa9cca
Show file tree
Hide file tree
Showing 32 changed files with 1,006 additions and 714 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-ravens-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vitest-pool-workers": minor
---

Support Vitest v3. While this drops testing for Vitest v2, we expect Vitest v2 will continue to work as well.
49 changes: 18 additions & 31 deletions fixtures/additional-modules/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,19 @@ import os from "node:os";
import path from "node:path";
import { setTimeout } from "node:timers/promises";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import {
runWranglerDev,
wranglerEntryPath,
} from "../../shared/src/run-wrangler-long-lived";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { unstable_startWorker } from "wrangler";
import { wranglerEntryPath } from "../../shared/src/run-wrangler-long-lived";

async function getTmpDir() {
return fs.mkdtemp(path.join(os.tmpdir(), "wrangler-modules-"));
}

type WranglerDev = Awaited<ReturnType<typeof runWranglerDev>>;
type WranglerDev = Awaited<ReturnType<typeof unstable_startWorker>>;
function get(worker: WranglerDev, pathname: string) {
const url = `http://${worker.ip}:${worker.port}${pathname}`;
const url = `http://example.com${pathname}`;
// Disable Miniflare's pretty error page, so we can parse errors as JSON
return fetch(url, { headers: { "MF-Disable-Pretty-Error": "true" } });
}

async function retry<T>(closure: () => Promise<T>, max = 30): Promise<T> {
for (let attempt = 1; attempt <= max; attempt++) {
try {
return await closure();
} catch (e) {
if (attempt === max) throw e;
}
await setTimeout(1_000);
}
assert.fail("Unreachable");
return worker.fetch(url, { headers: { "MF-Disable-Pretty-Error": "true" } });
}

describe("find_additional_modules dev", () => {
Expand All @@ -52,10 +38,12 @@ describe("find_additional_modules dev", () => {
path.join(tmpDir, "wrangler.toml")
);

worker = await runWranglerDev(tmpDir, ["--port=0", "--inspector-port=0"]);
worker = await unstable_startWorker({
config: path.join(tmpDir, "wrangler.toml"),
});
});
afterAll(async () => {
await worker.stop();
await worker.dispose();
try {
await fs.rm(tmpDir, { recursive: true, force: true });
} catch (e) {
Expand Down Expand Up @@ -102,28 +90,27 @@ describe("find_additional_modules dev", () => {
path.join(srcDir, "dynamic.js"),
'export default "new dynamic";'
);
await retry(async () => {
await vi.waitFor(async () => {
const res = await get(worker, "/dynamic");
assert.strictEqual(await res.text(), "new dynamic");
});

// Delete dynamically imported file
await fs.rm(path.join(srcDir, "lang", "en.js"));
const res = await retry(async () => {
const res = await get(worker, "/lang/en");
assert.strictEqual(res.status, 500);
return res;

await vi.waitFor(async () => {
await expect(get(worker, "/lang/en")).rejects.toThrowError(
'No such module "lang/en.js".'
);
});
const error = (await res.json()) as { message?: string };
expect(error.message).toBe('No such module "lang/en.js".');

// Create new dynamically imported file in new directory
await fs.mkdir(path.join(srcDir, "lang", "en"));
await fs.writeFile(
path.join(srcDir, "lang", "en", "us.js"),
'export default { hello: "hey" };'
);
await retry(async () => {
await vi.waitFor(async () => {
const res = await get(worker, "/lang/en/us");
assert.strictEqual(await res.text(), "hey");
});
Expand All @@ -133,7 +120,7 @@ describe("find_additional_modules dev", () => {
path.join(srcDir, "lang", "en", "us.js"),
'export default { hello: "bye" };'
);
await retry(async () => {
await vi.waitFor(async () => {
const res = await get(worker, "/lang/en/us");
assert.strictEqual(await res.text(), "bye");
});
Expand Down
2 changes: 1 addition & 1 deletion fixtures/asset-config/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default defineWorkersConfig({
server: {
deps: {
// Vitest automatically adds `/^(?!.*node_modules).*\.mjs$/` as an
// `inline` RegExp: https://github.com/vitest-dev/vitest/blob/v2.1.1/packages/vitest/src/constants.ts#L9
// `inline` RegExp: https://github.com/vitest-dev/vitest/blob/v3.0.5/packages/vitest/src/constants.ts#L9
// We'd like `packages/vitest-pool-workers/dist/pool/index.mjs` to be
// externalised though. Unfortunately, `inline`s are checked before
// `external`s, so there's no nice way we can override this. Instead,
Expand Down
Loading

0 comments on commit aaa9cca

Please sign in to comment.