Skip to content

Commit

Permalink
feat: implement fetch and add local-mode-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rozenmd committed Jul 4, 2022
1 parent 6732d95 commit fc9d99a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 34 deletions.
60 changes: 32 additions & 28 deletions fixtures/local-mode-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
{
"name": "local-mode-tests",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"check:type": "tsc",
"test": "npx jest --forceExit"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.2.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"jest": {
"restoreMocks": true,
"testTimeout": 30000,
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
"transform": {
"^.+\\.c?(t|j)sx?$": [
"esbuild-jest",
{
"sourcemap": true
}
]
}
}
"name": "local-mode-tests",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"check:type": "tsc",
"test": "npx jest --forceExit"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.2.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"jest": {
"restoreMocks": true,
"testTimeout": 30000,
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
"transformIgnorePatterns": [
"node_modules/(?!find-up|locate-path|p-locate|p-limit|p-timeout|p-queue|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream|get-port|supports-color|pretty-bytes)",
"wrangler-dist/cli.js"
],
"transform": {
"^.+\\.c?(t|j)sx?$": [
"esbuild-jest",
{
"sourcemap": true
}
]
}
}
}
5 changes: 5 additions & 0 deletions fixtures/local-mode-tests/src/basicModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch() {
return new Response("Hello World!");
},
};
18 changes: 18 additions & 0 deletions fixtures/local-mode-tests/tests/unstableDev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import wrangler from "wrangler";

describe("worker", () => {
it.only("should invoke the worker and exit", async () => {
//since the script is invoked from the directory above, need to specify index.js is in src/
const worker = await wrangler.unstable_dev("src/basicModule.ts", {
ip: "127.0.0.1",
port: 1337,
});

const resp = await worker.fetch();
const text = await resp.text();

expect(text).toMatchInlineSnapshot(`"Hello World!"`);

await worker.stop();
});
});
1 change: 1 addition & 0 deletions fixtures/local-mode-tests/wrangler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "wrangler";
10 changes: 8 additions & 2 deletions packages/wrangler/src/api/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { startDev } from "../dev";
import { logger } from "../logger";

import type { Response } from "undici";

interface DevOptions {
env?: string;
ip?: string;
Expand All @@ -20,7 +23,10 @@ export async function unstable_dev(script: string, options: DevOptions) {
`unstable_dev() is experimental\nunstable_dev()'s behaviour will likely change in future releases`
);

return new Promise<{ stop: () => void }>((resolve) => {
return new Promise<{
stop: () => void;
fetch: () => Promise<Response | undefined>;
}>((resolve) => {
//lmao
return new Promise<Awaited<ReturnType<typeof startDev>>>((ready) => {
const devServer = startDev({
Expand All @@ -33,7 +39,7 @@ export async function unstable_dev(script: string, options: DevOptions) {
showInteractiveDevSession: false,
});
}).then((devServer) => {
resolve({ stop: devServer.stop });
resolve({ stop: devServer.stop, fetch: devServer.fetch });
});
});
}
2 changes: 1 addition & 1 deletion packages/wrangler/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { main } from ".";
* The main entrypoint for the CLI.
* main only gets called when the script is run directly, not when it's imported as a module.
*/
if (require.main) {
if (typeof jest === "undefined" && require.main) {
main(hideBin(process.argv)).catch((e) => {
// The logging of any error that was thrown from `main()` is handled in the `yargs.fail()` handler.
// Here we just want to ensure that the process exits with a non-zero code.
Expand Down
14 changes: 12 additions & 2 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { watch } from "chokidar";
import getPort from "get-port";
import { render } from "ink";
import React from "react";
import { fetch } from "undici";
import { findWranglerToml, printBindings, readConfig } from "./config";
import Dev from "./dev/dev";
import { getVarsForDev } from "./dev/dev-vars";
Expand Down Expand Up @@ -506,9 +507,18 @@ export async function startDev(args: ArgumentsCamelCase<DevArgs>) {
watcher,
stop: async () => {
devReactElement.unmount();
await devReactElement.waitUntilExit();
await watcher?.close();
process.exit(0);
},
fetch: async () => {
const port = args.port || config.dev.port || (await getLocalPort());
const address = args.ip || config.dev.ip || "localhost";
let data;
try {
data = await fetch(`http://${address}:${port}/`);
} catch (err) {
console.log("err: ", err);
}
return data;
},
};
} 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, 200));
await new Promise((resolve) => setTimeout(resolve, 5000));
onReady();
}

Expand Down

0 comments on commit fc9d99a

Please sign in to comment.