-
Notifications
You must be signed in to change notification settings - Fork 759
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
penalosa
wants to merge
1
commit into
main
Choose a base branch
from
penalosa/node-test-fixtures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", () => { | ||
/** | ||
* @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
51
fixtures/miniflare-node-test/src/index-with-bindings.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
fixtures/miniflare-node-test/src/index-with-imports.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function sayHello(url: string) { | ||
return `Hello from ${url}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?)