Skip to content

Commit

Permalink
make it possible to add fetch init options
Browse files Browse the repository at this point in the history
  • Loading branch information
rozenmd committed Jul 4, 2022
1 parent 7e7b0fd commit 0aa9f6b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
22 changes: 21 additions & 1 deletion .changeset/quiet-olives-fry.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@
"wrangler": patch
---

feat: implement fetch, and write our first integration test
feat: implement fetch for wrangler's unstable_dev API, and write our first integration test.

Prior to this PR, users of `unstable_dev` had to provide their own fetcher, and guess the address and port that the wrangler dev server was using.

With this implementation, it's now possible to test wrangler, using just wrangler (and a test framework):

```js
describe("worker", async () => {
const worker = await wrangler.unstable_dev("src/index.ts");

const resp = await worker.fetch();

expect(resp).not.toBe(undefined);
if (resp) {
const text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Hello World!"`);
}

worker.stop();
}
```
Closes #1383
Closes #1384
Expand Down
5 changes: 4 additions & 1 deletion fixtures/local-mode-tests/tests/unstableDev.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import wrangler from "wrangler";

describe("worker", () => {
let worker: { fetch: () => Promise<Response | undefined>; stop: () => void };
let worker: {
fetch: (init?: RequestInit) => Promise<Response | undefined>;
stop: () => void;
};

beforeAll(async () => {
//since the script is invoked from the directory above, need to specify index.js is in src/
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/api/dev.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { startDev } from "../dev";
import { logger } from "../logger";

import type { Response } from "undici";
import type { RequestInit, Response } from "undici";

interface DevOptions {
env?: string;
Expand Down Expand Up @@ -29,7 +29,7 @@ export async function unstable_dev(script: string, options: DevOptions) {

return new Promise<{
stop: () => void;
fetch: () => Promise<Response | undefined>;
fetch: (init?: RequestInit) => Promise<Response | undefined>;
}>((resolve) => {
//lmao
return new Promise<Awaited<ReturnType<typeof startDev>>>((ready) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {

import type { Config } from "./config";
import type { Route } from "./config/environment";
import type { RequestInit } from "undici";
import type { Argv, ArgumentsCamelCase } from "yargs";

interface DevArgs {
Expand Down Expand Up @@ -509,11 +510,11 @@ export async function startDev(args: ArgumentsCamelCase<DevArgs>) {
devReactElement.unmount();
await watcher?.close();
},
fetch: async () => {
fetch: async (init?: RequestInit) => {
const port = args.port || config.dev.port || (await getLocalPort());
const address = args.ip || config.dev.ip || "localhost";

return await fetch(`http://${address}:${port}/`);
return await fetch(`http://${address}:${port}/`, init);
},
};
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function useLocalWorker({
});
//TODO: instead of being lucky with spawn's timing, have miniflare-cli notify wrangler that it's ready in packages/wrangler/src/miniflare-cli/index.ts, after the mf.startScheduler promise resolves
if (onReady) {
await new Promise((resolve) => setTimeout(resolve, 400));
await new Promise((resolve) => setTimeout(resolve, 500));
onReady();
}

Expand Down

0 comments on commit 0aa9f6b

Please sign in to comment.