2024-04-16 18:19:34 -04:00
|
|
|
// test/index.spec.ts
|
2024-10-24 12:44:38 -06:00
|
|
|
import {
|
|
|
|
createExecutionContext,
|
|
|
|
env,
|
|
|
|
SELF,
|
|
|
|
waitOnExecutionContext,
|
|
|
|
} from "cloudflare:test"
|
|
|
|
import { describe, expect, it } from "vitest"
|
|
|
|
import worker from "../src/index"
|
2024-04-16 18:19:34 -04:00
|
|
|
|
|
|
|
// For now, you'll need to do something like this to get a correctly-typed
|
|
|
|
// `Request` to pass to `worker.fetch()`.
|
2024-10-24 12:44:38 -06:00
|
|
|
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>
|
2024-04-16 18:19:34 -04:00
|
|
|
|
|
|
|
describe("Hello World worker", () => {
|
2024-10-24 12:44:38 -06:00
|
|
|
it("responds with Hello World! (unit style)", async () => {
|
|
|
|
const request = new IncomingRequest("http://example.com")
|
|
|
|
// Create an empty context to pass to `worker.fetch()`.
|
|
|
|
const ctx = createExecutionContext()
|
|
|
|
const response = await worker.fetch(request, env, ctx)
|
|
|
|
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
|
|
|
await waitOnExecutionContext(ctx)
|
|
|
|
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`)
|
|
|
|
})
|
2024-04-16 18:19:34 -04:00
|
|
|
|
2024-10-24 12:44:38 -06:00
|
|
|
it("responds with Hello World! (integration style)", async () => {
|
|
|
|
const response = await SELF.fetch("https://example.com")
|
|
|
|
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`)
|
|
|
|
})
|
|
|
|
})
|