Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions .changeset/gold-paws-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vitest-pool-workers": minor
---

Infer latest locally available compatibility date if none is provided in the test configuration
5 changes: 5 additions & 0 deletions .changeset/true-groups-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vitest-pool-workers": patch
---

ensure that vitest specific nodejs polyfills override native modules
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "ai-vectorize",
"main": "src/index.ts",
// compatibility_date is required so we can run `wrangler build`
"compatibility_date": "2025-03-20",
"ai": {
"binding": "AI",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "basics-integration-auxiliary",
"main": "src/index.ts",
// compatibility_date is required so we can run `wrangler build`
"compatibility_date": "2024-01-01",
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ it("dispatches fetch event", async () => {
// Importantly, it uses the exact `import("../src").default` instance we could
// import in this file as its handler.
const response = await SELF.fetch("http://example.com");
expect(await response.text()).toBe("👋 http://example.com");
expect(await response.text()).toBe("👋 http://example.com/");
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ it("dispatches fetch event", async () => {
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(await response.text()).toBe("👋 http://example.com");
expect(await response.text()).toBe("👋 http://example.com/");
});

it("calls arbitrary function", () => {
const request = new Request("http://example.com");
expect(greet(request)).toBe("👋 http://example.com");
expect(greet(request)).toBe("👋 http://example.com/");
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "basics-unit-integration-self",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "container-app",
"main": "src/index.ts",
"compatibility_date": "2025-04-03",
// don't provide compatibility_date so that vitest will infer the latest one
"containers": [
{
"image": "./Dockerfile",
Expand Down
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/d1/wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "d1",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"env": {
"production": {
"d1_databases": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "durable-objects",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"durable_objects": {
"bindings": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hyperdrive",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"hyperdrive": [
{
"binding": "ECHO_SERVER_HYPERDRIVE",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "images",
"main": "src/index.ts",
"compatibility_date": "2025-02-04",
// don't provide compatibility_date so that vitest will infer the latest one
"images": {
"binding": "IMAGES",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kv-r2-caches",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"kv_namespaces": [
{
"binding": "KV_NAMESPACE",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { afterEach, beforeEach, expect, it, vi } from "vitest";

it("runs in Node.js compatibility mode", () => {
expect(typeof process).toBe("object");
expect(process.versions).toBeDefined();
expect(process.versions.node).toBeDefined();
expect(typeof Buffer).toBe("function");
});
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ it("requires next property to call next()", async () => {
const fn: BareFunction = (ctx) => ctx.next();
const request = new IncomingRequest("https://example.com");
const ctx = createPagesEventContext<typeof fn>({ request });
expect(fn(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
await expect(fn(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`[TypeError: Cannot call \`EventContext#next()\` without including \`next\` property in 2nd argument to \`createPagesEventContext()\`]`
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineWorkersProject({
"CONFIG_NESTED.DEFINED.THING": "[1,2,3]",
},
test: {
exclude: ["test/assets.test.ts"],
exclude: ["test/assets.test.ts", "test/nodejs.test.ts"],
poolOptions: {
workers: {
singleWorker: true,
Expand Down
16 changes: 16 additions & 0 deletions fixtures/vitest-pool-workers-examples/misc/vitest.nodejs.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersProject({
test: {
name: "misc-nodejs",
include: ["test/nodejs.test.ts"],
poolOptions: {
workers: {
singleWorker: true,
wrangler: {
configPath: "./wrangler.nodejs.jsonc",
},
},
},
},
});
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/misc/wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "../node_modules/wrangler/config-schema.json",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"define": {
"WRANGLER_DEFINED_THING": "\"thing\"",
"WRANGLER_NESTED.DEFINED.THING": "[1,2,3]",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "../node_modules/wrangler/config-schema.json",
// don't provide compatibility_date so that vitest will infer the latest one
"compatibility_flags": ["nodejs_compat"],
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "module-resolution",
"main": "src/index.ts",
"compatibility_date": "2024-04-05",
// don't provide compatibility_date so that vitest will infer the latest one
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "api-service",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"services": [
{
"binding": "AUTH_SERVICE",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "auth-service",
"main": "src/index.ts",
// compatibility_date is required so we can run `wrangler build`
"compatibility_date": "2024-01-01",
/*
`AUTH_PUBLIC_KEY` would be a secret created with `wrangler secret put`, and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "database-service",
"main": "src/index.ts",
// compatibility_date is required so we can run `wrangler build`
"compatibility_date": "2024-01-01",
"kv_namespaces": [
{
Expand Down
8 changes: 4 additions & 4 deletions fixtures/vitest-pool-workers-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"type": "module",
"scripts": {
"check:type": "node tsc-all.mjs",
"list": "vitest --config vitest.workers.config.ts list",
"test": "vitest --config vitest.workers.config.ts",
"list": "vitest list",
"test": "vitest",
"test:ci": "run-script-os",
"test:ci:default": "vitest run --config vitest.workers.config.ts",
"test:ci:win32": "vitest run --config vitest.workers.config.ts --exclude test/sqlite-in-do.test.ts"
"test:ci:default": "vitest run",
"test:ci:win32": "vitest run --exclude test/sqlite-in-do.test.ts"
},
"devDependencies": {
"@cloudflare/containers": "^0.0.25",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "pages-with-config",
"compatibility_date": "2024-09-19",
// don't provide compatibility_date so that vitest will infer the latest one
"pages_build_output_dir": "public",
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pipelines",
"main": "src/index.ts",
"compatibility_date": "2025-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"pipelines": [
{
"binding": "PIPELINE",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "queues",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
"kv_namespaces": [
{
"binding": "QUEUE_RESULTS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "request-mocking",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
}
30 changes: 15 additions & 15 deletions fixtures/vitest-pool-workers-examples/rpc/test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("named entrypoints", () => {
],
"method": "GET",
"source": "testNamedHandler",
"url": "https://example.com",
"url": "https://example.com/",
}
`);
});
Expand All @@ -31,19 +31,19 @@ describe("named entrypoints", () => {
"https://example.com"
);
expect(await response.json()).toMatchInlineSnapshot(`
{
"ctxWaitUntil": "function",
"envKeys": [
"KV_NAMESPACE",
"TEST_NAMED_ENTRYPOINT",
"TEST_NAMED_HANDLER",
"TEST_OBJECT",
],
"method": "GET",
"source": "TestNamedEntrypoint",
"url": "https://example.com",
}
`);
{
"ctxWaitUntil": "function",
"envKeys": [
"KV_NAMESPACE",
"TEST_NAMED_ENTRYPOINT",
"TEST_NAMED_HANDLER",
"TEST_OBJECT",
],
"method": "GET",
"source": "TestNamedEntrypoint",
"url": "https://example.com/",
}
`);
});
it("calls method with rpc", async () => {
const result = await env.TEST_NAMED_ENTRYPOINT.ping();
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("Durable Object", () => {
],
"method": "GET",
"source": "TestObject",
"url": "https://example.com",
"url": "https://example.com/",
}
`);
});
Expand Down
3 changes: 1 addition & 2 deletions fixtures/vitest-pool-workers-examples/rpc/wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "rpc",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
"compatibility_flags": ["rpc"],
// don't provide compatibility_date so that vitest will infer the latest one
"kv_namespaces": [
{
"binding": "KV_NAMESPACE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FilteredPushArray<T> extends Array<T> {

export default defineConfig({
test: {
projects: ["*/vitest.*config.*ts"],
globalSetup: ["./vitest.global.ts"],
// Configure the `vite-node` server used by Vitest code to import configs,
// custom pools and tests. By default, Vitest effectively applies Vite
Expand Down
1 change: 0 additions & 1 deletion fixtures/vitest-pool-workers-examples/vitest.workspace.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "web-assembly",
"main": "src/index.ts",
"compatibility_date": "2024-01-01",
// don't provide compatibility_date so that vitest will infer the latest one
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workers-static-assets-only",
"compatibility_date": "2024-09-19",
// don't provide compatibility_date so that vitest will infer the latest one
"assets": {
"directory": "./public",
"html_handling": "none",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workers-static-assets-with-user-worker-run-first",
"main": "src/index.ts",
"compatibility_date": "2024-09-19",
// don't provide compatibility_date so that vitest will infer the latest one
"assets": {
"directory": "./public",
"binding": "ASSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workers-static-assets-with-user-worker",
"main": "src/index.ts",
"compatibility_date": "2024-09-19",
// don't provide compatibility_date so that vitest will infer the latest one
"assets": {
"directory": "./public",
"binding": "ASSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workflows",
"main": "src/index.ts",
"compatibility_date": "2025-02-04",
// don't provide compatibility_date so that vitest will infer the latest one
"workflows": [
{
"binding": "MODERATOR",
Expand Down
4 changes: 3 additions & 1 deletion packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,9 @@ export class Miniflare {
this.#log.debug(`Unable to remove temporary directory: ${String(e)}`);
}
// Unregister all workers from the dev registry
void this.#devRegistry.dispose();
this.#devRegistry.dispose()?.catch((e) => {
this.#log.debug(`Error disposing Dev Registry: ${getErrorMessage(e)}`);
});
});

this.#disposeController = new AbortController();
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest-pool-workers/scripts/bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ await esbuild.build({
});

// Build pool, worker and libs
const libPaths = Array.from(walk(path.join(pkgRoot, "src", "worker", "lib")));
const libPaths = [
...walk(path.join(pkgRoot, "src/worker/lib")),
...walk(path.join(pkgRoot, "src/worker/node")),
];

/** @type {import("esbuild").BuildOptions} */
const commonOptions = {
Expand Down
Loading
Loading