@@ -2,6 +2,31 @@ import { Hono } from '../../hono'
2
2
import { handle } from './handler'
3
3
import type { FetchEvent } from './types'
4
4
5
+ beforeAll ( ( ) => {
6
+ // fetch errors when it's not bound to globalThis in service worker
7
+ // set a fetch stub to emulate that behavior
8
+ vi . stubGlobal (
9
+ 'fetch' ,
10
+ function fetch ( this : undefined | typeof globalThis , arg0 : string | Request ) {
11
+ if ( this !== globalThis ) {
12
+ const error = new Error (
13
+ // eslint-disable-next-line quotes
14
+ "Failed to execute 'fetch' on 'WorkerGlobalScope': Illegal invocation"
15
+ )
16
+ error . name = 'TypeError'
17
+ throw error
18
+ }
19
+ if ( arg0 instanceof Request && arg0 . url === 'http://localhost/fallback' ) {
20
+ return new Response ( 'hello world' )
21
+ }
22
+ return Response . error ( )
23
+ }
24
+ )
25
+ } )
26
+ afterAll ( ( ) => {
27
+ vi . unstubAllGlobals ( )
28
+ } )
29
+
5
30
describe ( 'handle' , ( ) => {
6
31
it ( 'Success to fetch' , async ( ) => {
7
32
const app = new Hono ( )
@@ -20,6 +45,19 @@ describe('handle', () => {
20
45
expect ( json ) . toStrictEqual ( { hello : 'world' } )
21
46
} )
22
47
it ( 'Fallback 404' , async ( ) => {
48
+ const app = new Hono ( )
49
+ const handler = handle ( app )
50
+ const text = await new Promise < Response > ( ( resolve ) => {
51
+ handler ( {
52
+ request : new Request ( 'http://localhost/fallback' ) ,
53
+ respondWith ( res ) {
54
+ resolve ( res )
55
+ } ,
56
+ } as FetchEvent )
57
+ } ) . then ( ( res ) => res . text ( ) )
58
+ expect ( text ) . toBe ( 'hello world' )
59
+ } )
60
+ it ( 'Fallback 404 with explicit fetch' , async ( ) => {
23
61
const app = new Hono ( )
24
62
const handler = handle ( app , {
25
63
async fetch ( ) {
0 commit comments