Skip to content
Myr

handler.test.ts

This file

Path

src/adapter/service-worker/handler.test.ts

121 lines · 3.4 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–121why, one step back
1import { Hono } from '../../hono'
2import { handle } from './handler'3import type { FetchEvent } from './types'45beforeAll(() => {6 // fetch errors when it's not bound to globalThis in service worker7 // set a fetch stub to emulate that behavior8 vi.stubGlobal(9 'fetch',10 function fetch(this: undefined | typeof globalThis, arg0: string | Request) {11 if (this !== globalThis) {12 const error = new Error(13 "Failed to execute 'fetch' on 'WorkerGlobalScope': Illegal invocation"14 )15 error.name = 'TypeError'16 throw error17 }18 if (arg0 instanceof Request && arg0.url === 'http://localhost/fallback') {19 return new Response('hello world')20 }21 return Response.error()22 }23 )24})25afterAll(() => {26 vi.unstubAllGlobals()27})2829describe('handle', () => {30 it('Success to fetch', async () => {31 const app = new Hono()32 app.get('/', (c) => {33 return c.json({ hello: 'world' })34 })35 const handler = handle(app)36 const json = await new Promise<Response>((resolve) => {37 handler({38 request: new Request('http://localhost/'),39 respondWith(res) {40 resolve(res)41 },42 } as FetchEvent)43 }).then((res) => res.json())44 expect(json).toStrictEqual({ hello: 'world' })45 })46 it('Fallback 404', async () => {47 const app = new Hono()48 const handler = handle(app)49 const text = await new Promise<Response>((resolve) => {50 handler({51 request: new Request('http://localhost/fallback'),52 respondWith(res) {53 resolve(res)54 },55 } as FetchEvent)56 }).then((res) => res.text())57 expect(text).toBe('hello world')58 })59 it('Fallback 404 with explicit fetch', async () => {60 const app = new Hono()61 const handler = handle(app, {62 async fetch() {63 return new Response('hello world')64 },65 })66 const text = await new Promise<Response>((resolve) => {67 handler({68 request: new Request('http://localhost/'),69 respondWith(res) {70 resolve(res)71 },72 } as FetchEvent)73 }).then((res) => res.text())74 expect(text).toBe('hello world')75 })76 it('Do not fallback 404 when fetch is undefined', async () => {77 const app = new Hono()78 app.get('/', (c) => c.text('Not found', 404))79 const handler = handle(app, {80 fetch: undefined,81 })82 const result = await new Promise<Response>((resolve) =>83 handler({84 request: new Request('https://localhost/'),85 respondWith(r) {86 resolve(r)87 },88 } as FetchEvent)89 )9091 expect(result.status).toBe(404)92 expect(await result.text()).toBe('Not found')93 })9495 it('Should pass FetchEvent as second argument to app.fetch', async () => {96 const app = new Hono()9798 app.get('/', (c) => {99 return c.json({100 // @ts-expect-error executionCtx is FetchEvent but not typed well101 clientId: c.executionCtx.clientId,102 })103 })104105 const handler = handle(app)106 // @ts-expect-error Force mocking FetchEvent including custom values107 const mockFetchEvent = {108 clientId: 'test-client-id',109 respondWith: vi.fn(),110 request: new Request('http://localhost'),111 } as FetchEvent112113 const response = await new Promise<Response>((resolve) => {114 mockFetchEvent.respondWith = resolve115 handler(mockFetchEvent)116 })117118 const json = await response.json()119 expect(json.clientId).toBe('test-client-id')120 })121})