-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: testing scheduled events with
wrangler dev
remote mode (#1815)
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
1 parent
89362e0
commit d8fe95d
Showing
12 changed files
with
890 additions
and
762 deletions.
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,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 |
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
135 changes: 135 additions & 0 deletions
135
packages/wrangler/src/__tests__/middleware.scheduled.test.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,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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.