Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixtures using startWorker() and Miniflare directly #7802

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions fixtures/miniflare-node-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "miniflare-node-test",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"type": "module",
"scripts": {
"test:ci": "node --test"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20241230.0",
"@types/is-even": "^1.0.2",
"is-even": "^1.0.0",
"miniflare": "workspace:*",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
41 changes: 41 additions & 0 deletions fixtures/miniflare-node-test/src/index-build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from "node:assert";
import { spawnSync } from "node:child_process";
import test, { after, before, describe } from "node:test";
import { Miniflare } from "miniflare";

before(() => {
spawnSync("npx wrangler build -c wrangler-build.json", {
shell: true,
stdio: "pipe",
});
});

describe("worker build", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice PR!

nit: "worker build" and "worker" names in the next file are not very descriptive.

(question: how to use a different worked when interacting with Miniflare? I don't see an option so I guess MINIFLARE_WORKERD_VERSION is respected?)

/**
* @type {Miniflare}
*/
let worker;

before(async () => {
worker = new Miniflare({
modules: [
{
type: "ESModule",
path: "dist/index.js",
},
],
});
await worker.ready;
});

test("even", async () => {
assert.strictEqual(
await (await worker.dispatchFetch("http://example.com?number=2")).text(),
"even"
);
});

after(async () => {
await worker.dispose();
});
});
51 changes: 51 additions & 0 deletions fixtures/miniflare-node-test/src/index-with-bindings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import assert from "node:assert";
import test, { after, before, describe } from "node:test";
import { Miniflare } from "miniflare";

describe("worker", () => {
/**
* @type {Miniflare}
*/
let worker;

before(async () => {
worker = new Miniflare({
modules: [
{
type: "ESModule",
path: "src/index.js",
},
],
bindings: {
FOO: "Hello Bindings",
},
kvNamespaces: ["KV"],
});
await worker.ready;
});

test("hello world", async () => {
assert.strictEqual(
await (await worker.dispatchFetch("http://example.com")).text(),
"Hello World"
);
});

test("text binding", async () => {
const bindings = await worker.getBindings();
assert.strictEqual(bindings.FOO, "Hello Bindings");
});

test("kv binding", async () => {
/**
* @type {{KV: import("@cloudflare/workers-types/experimental").KVNamespace, FOO: string}}
*/
const bindings = await worker.getBindings();
await bindings.KV.put("key", "value");
assert.strictEqual(await bindings.KV.get("key"), "value");
});

after(async () => {
await worker.dispose();
});
});
12 changes: 12 additions & 0 deletions fixtures/miniflare-node-test/src/index-with-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { sayHello } from "./say-hello.js";

export default {
/**
*
* @param {Request} request
* @returns
*/
async fetch(request) {
return new Response(sayHello(request.url));
},
};
34 changes: 34 additions & 0 deletions fixtures/miniflare-node-test/src/index-with-imports.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from "node:assert";
import test, { after, before, describe } from "node:test";
import { Miniflare } from "miniflare";

describe("worker", () => {
/**
* @type {Miniflare}
*/
let worker;

before(async () => {
worker = new Miniflare({
scriptPath: "src/index-with-imports.js",
modules: true,
modulesRules: [{ type: "ESModule", include: ["**/*.js"] }],
});
try {
await worker.ready;
} catch (e) {
console.error(e);
}
});

test("hello world", async () => {
assert.strictEqual(
await (await worker.dispatchFetch("http://example.com/path")).text(),
"Hello from http://example.com/path"
);
});

after(async () => {
await worker.dispose();
});
});
10 changes: 10 additions & 0 deletions fixtures/miniflare-node-test/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
/**
*
* @param {Request} request
* @returns
*/
async fetch(request) {
return new Response(`Hello World`);
},
};
33 changes: 33 additions & 0 deletions fixtures/miniflare-node-test/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import assert from "node:assert";
import test, { after, before, describe } from "node:test";
import { Miniflare } from "miniflare";

describe("worker", () => {
/**
* @type {Miniflare}
*/
let worker;

before(async () => {
worker = new Miniflare({
modules: [
{
type: "ESModule",
path: "src/index.js",
},
],
});
await worker.ready;
});

test("hello world", async () => {
assert.strictEqual(
await (await worker.dispatchFetch("http://example.com")).text(),
"Hello World"
);
});

after(async () => {
await worker.dispose();
});
});
10 changes: 10 additions & 0 deletions fixtures/miniflare-node-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isEven from "is-even";

export default {
async fetch(request): Promise<Response> {
const url = new URL(request.url);
return new Response(
isEven(Number(url.searchParams.get("number") ?? "0")) ? "even" : "odd"
);
},
} satisfies ExportedHandler;
8 changes: 8 additions & 0 deletions fixtures/miniflare-node-test/src/say-hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
* @param {string} url
* @returns
*/
export function sayHello(url) {
return `Hello from ${url}`;
}
8 changes: 8 additions & 0 deletions fixtures/miniflare-node-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
"checkJs": true
},
"include": ["**/*.js", "**/*.ts"]
}
5 changes: 5 additions & 0 deletions fixtures/miniflare-node-test/wrangler-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "build-miniflare",
"main": "src/index.ts",
"compatibility_date": "2025-01-16"
}
6 changes: 6 additions & 0 deletions fixtures/miniflare-node-test/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "unstable_dev"
main = "dist/out.js"
compatibility_date = "2022-12-08"

[build]
command = "npx esbuild src/index.js --outfile=dist/out.js"
22 changes: 22 additions & 0 deletions fixtures/start-worker-node-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "miniflare-node-test",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"type": "module",
"scripts": {
"test:ci": "node --test"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20241230.0",
"@types/is-even": "^1.0.2",
"is-even": "^1.0.0",
"miniflare": "workspace:*",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
25 changes: 25 additions & 0 deletions fixtures/start-worker-node-test/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from "node:assert";
import test, { after, before, describe } from "node:test";
import { unstable_startWorker } from "wrangler";

describe("worker", () => {
/**
* @type {Awaited<ReturnType<typeof unstable_startWorker>>}
*/
let worker;

before(async () => {
worker = await unstable_startWorker({ config: "wrangler.json" });
});

test("hello world", async () => {
assert.strictEqual(
await (await worker.fetch("http://example.com")).text(),
"Hello from even"
);
});

after(async () => {
await worker.dispose();
});
});
13 changes: 13 additions & 0 deletions fixtures/start-worker-node-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import isEven from "is-even";
import { sayHello } from "./say-hello";

export default {
async fetch(request): Promise<Response> {
const url = new URL(request.url);
return new Response(
sayHello(
isEven(Number(url.searchParams.get("number") ?? "0")) ? "even" : "odd"
)
);
},
} satisfies ExportedHandler;
3 changes: 3 additions & 0 deletions fixtures/start-worker-node-test/src/say-hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sayHello(url: string) {
return `Hello from ${url}`;
}
8 changes: 8 additions & 0 deletions fixtures/start-worker-node-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
"checkJs": true
},
"include": ["**/*.js", "**/*.ts"]
}
5 changes: 5 additions & 0 deletions fixtures/start-worker-node-test/wrangler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "build-miniflare",
"main": "src/index.ts",
"compatibility_date": "2025-01-16"
}
Loading
Loading