Skip to content
Myr

test(hono-base): add tests for covering 100% (#2952)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 7eb3e3b · Browse files · 1 file+65 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+65 added−2 removed

  • tests:+65 added−2 removed

1 file+65 added−2 removed

modified:src/hono.test.ts+65 added-2 removed
1386 unmodified lines
1387 app.get('/error', () => {1388 throw new Error('This is Error')1389 })13901391 app.get('/error-string', () => {1392 throw 'This is Error'1393 })13941395 app.use('/error-middleware', async () => {1396 throw new Error('This is Middleware Error')1397 })1398
1 unmodified line
1400 c.header('x-debug', err.message)1401 return c.text('Custom Error Message', 500)1402 })14031404 it('Should throw Error if a non-Error object is thrown in a handler', async () => {1405 expect(() => app.request('/error-string')).toThrowError()1406 })14071408 it('Custom Error Message', async () => {1409 let res = await app.request('https://example.com/error')1410 expect(res.status).toBe(500)1411 expect(await res.text()).toBe('Custom Error Message')
1100 unmodified lines
2512})25132514describe('app.mount()', () => {2515 describe('Basic', () => {2508 const anotherApp = (req: Request, params: unknown) => {2516 const anotherApp = (req: Request, ...params: unknown[]) => {2517 const path = getPath(req)2518 if (path === '/') {2519 return new Response('AnotherApp')2520 }
19 unmodified lines
2540 },2541 }2542 )2543 }2544 if (path === '/undefined') {2545 return undefined as unknown as Response2546 }2547 return new Response('Not Found from AnotherApp', {2548 status: 404,2549 })2550 }
3 unmodified lines
2554 await next()2555 c.header('x-message', 'Foo')2556 })2557 app.get('/', (c) => c.text('Hono'))2558 app.notFound((c) => {2559 return c.text('Not Found from App', 404)2560 })25612562 app.mount('/another-app', anotherApp, () => {2563 return 'params'2564 })2565 app.mount('/another-app-with-array-option', anotherApp, () => {2566 return ['param1', 'param2']2567 })2568 app.mount('/another-app2/sub-slash/', anotherApp)25692570 const api = new Hono().basePath('/api')2571 api.mount('/another-app', anotherApp)
37 unmodified lines
26092610 res = await app.request('/another-app/with-params')2611 expect(res.status).toBe(200)2612 expect(await res.json()).toEqual({2595 params: 'params',2613 params: ['params'],2614 })26152616 res = await app.request('/another-app/undefined')2617 expect(res.status).toBe(404)2618 expect(await res.text()).toBe('Not Found from App')2619 })26202621 it('Should return response from Another app with an array option', async () => {2622 const res = await app.request('/another-app-with-array-option/with-params')2623 expect(res.status).toBe(200)2624 expect(await res.json()).toEqual({2625 params: ['param1', 'param2'],2626 })2627 })26282629 it('Should return responses from AnotherApp - sub + slash', async () => {2630 const res = await app.request('/another-app2/sub-slash')2631 expect(res.status).toBe(200)2632 expect(await res.text()).toBe('AnotherApp')
152 unmodified lines
2785 (content: string | Promise<string>, head: { title: string }): Response | Promise<Response>2786 }2787}27882789describe('app.request()', () => {2790 it('Should return response with Request and RequestInit as args', async () => {2791 const app = new Hono()2792 app.get('/foo', (c) => {2793 return c.json(c.req.header('x-message'))2794 })2795 const req = new Request('http://localhost/foo')2796 const headers = new Headers()2797 headers.append('x-message', 'hello')2798 const res = await app.request(req, {2799 headers,2800 })2801 expect(res.status).toBe(200)2802 expect(await res.text()).toBe('"hello"')2803 })2804})28052806describe('app.fire()', () => {2807 it('Should call global.addEventListener', () => {2808 const app = new Hono()2809 const addEventListener = vi.fn()2810 global.addEventListener = addEventListener2811 app.fire()2812 expect(addEventListener).toHaveBeenCalledWith('fetch', expect.any(Function))28132814 const fetchEventListener = addEventListener.mock.calls[0][1]2815 const respondWith = vi.fn()2816 const request = new Request('http://localhost')2817 fetchEventListener({ respondWith, request })2818 expect(respondWith).toHaveBeenCalledWith(expect.any(Promise))2819 })2820})28212822describe('Context render and setRenderer', () => {2823 const app = new Hono()2824 app.get('/default', (c) => {2825 return c.render('<h1>content</h1>', { title: 'dummy ' })
762 unmodified lines