Skip to content

Commit

Permalink
feat: testing scheduled events with wrangler dev remote mode (#1815)
Browse files Browse the repository at this point in the history
Using the new middleware (#1735), we implement a way of testing scheduled workers from a fetch using `wrangler dev` in remote mode, by passing a new command line flag `--test-scheduled`. This exposes a route `/__scheduled` which will trigger the scheduled event.

```sh
$ npx wrangler dev index.js --test-scheduled

$ curl http://localhost:8787/__scheduled
```

Closes #570
  • Loading branch information
cameron-robey authored Sep 15, 2022
1 parent 89362e0 commit d8fe95d
Show file tree
Hide file tree
Showing 12 changed files with 890 additions and 762 deletions.
15 changes: 15 additions & 0 deletions .changeset/fair-chairs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"wrangler": patch
---

feat: testing scheduled events with `wrangler dev` remote mode

Using the new middleware (https://github.com/cloudflare/wrangler2/pull/1735), we implement a way of testing scheduled workers from a fetch using `wrangler dev` in remote mode, by passing a new command line flag `--test-scheduled`. This exposes a route `/__scheduled` which will trigger the scheduled event.

```sh
$ npx wrangler dev index.js --test-scheduled

$ curl http://localhost:8787/__scheduled
```

Closes https://github.com/cloudflare/wrangler2/issues/570
3 changes: 2 additions & 1 deletion packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,8 @@ describe("wrangler dev", () => {
--node-compat Enable node.js compatibility [boolean]
--persist Enable persistence for local mode, using default path: .wrangler/state [boolean]
--persist-to Specify directory to use for local persistence (implies --persist) [string]
--inspect Enable dev tools [deprecated] [boolean]",
--inspect Enable dev tools [deprecated] [boolean]
--test-scheduled Test scheduled events by visiting /__scheduled in browser [boolean] [default: false]",
"warn": "",
}
`);
Expand Down
135 changes: 135 additions & 0 deletions packages/wrangler/src/__tests__/middleware.scheduled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import * as fs from "node:fs";
import { unstable_dev } from "../api";
import { runInTempDir } from "./helpers/run-in-tmp";

jest.unmock("undici");

describe("run scheduled events with middleware", () => {
describe("module workers", () => {
runInTempDir();

beforeEach(() => {
const scriptContent = `
export default {
fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/__scheduled") {
return new Response("Fetch triggered at /__scheduled");
}
return new Response("Hello world!");
},
scheduled(controller, env, ctx) {
console.log("Doing something scheduled in modules...");
},
};
`;
fs.writeFileSync("index.js", scriptContent);
});

it("should not intercept when middleware is not enabled", async () => {
const worker = await unstable_dev(
"index.js",
{},
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/__scheduled");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Fetch triggered at /__scheduled"`);
await worker.stop();
});

it("should intercept when middleware is enabled", async () => {
const worker = await unstable_dev(
"index.js",
{ testScheduled: true },
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/__scheduled");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Ran scheduled event"`);
await worker.stop();
});

it("should not trigger scheduled event on wrong route", async () => {
const worker = await unstable_dev(
"index.js",
{ testScheduled: true },
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/test");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Hello world!"`);
await worker.stop();
});
});

describe("service workers", () => {
runInTempDir();

beforeEach(() => {
const scriptContent = `
addEventListener("scheduled", (event) => {
console.log("Doing something scheduled in service worker...");
});
addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
if (url.pathname === "/__scheduled") {
event.respondWith(new Response("Fetch triggered at /__scheduled"));
} else {
event.respondWith(new Response("Hello world!"));
}
});
`;
fs.writeFileSync("index.js", scriptContent);
});

it("should not intercept when middleware is not enabled", async () => {
const worker = await unstable_dev(
"index.js",
{},
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/__scheduled");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Fetch triggered at /__scheduled"`);
await worker.stop();
});

it("should intercept when middleware is enabled", async () => {
const worker = await unstable_dev(
"index.js",
{ testScheduled: true },
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/__scheduled");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Ran scheduled event"`);
await worker.stop();
});

it("should not trigger scheduled event on wrong route", async () => {
const worker = await unstable_dev(
"index.js",
{ testScheduled: true },
{ disableExperimentalWarning: true }
);

const resp = await worker.fetch("/test");
let text;
if (resp) text = await resp.text();
expect(text).toMatchInlineSnapshot(`"Hello world!"`);
await worker.stop();
});
});
});
Loading

0 comments on commit d8fe95d

Please sign in to comment.