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
Provenance
- Commit
e1e0e09- Tree
3127e83- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7eb3e3b
1 file changed+65 added−2 removed
modified:src/hono.test.ts+65 added-2 removed
1386 unmodified lines1387 app.get('/error', () => {1388 throw new Error('This is Error')1389 })1390 1391+ app.get('/error-string', () => {1392+ throw 'This is Error'1393+ })1394+1395 app.use('/error-middleware', async () => {1396 throw new Error('This is Middleware Error')1397 })1398 1 unmodified line1400 c.header('x-debug', err.message)1401 return c.text('Custom Error Message', 500)1402 })1403 1404+ it('Should throw Error if a non-Error object is thrown in a handler', async () => {1405+ expect(() => app.request('/error-string')).toThrowError()1406+ })1407+1408 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 lines2512 })2513 2514 describe('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 lines2540 },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 lines2554 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+ })2561+2562 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)2569 2570 const api = new Hono().basePath('/api')2571 api.mount('/another-app', anotherApp)37 unmodified lines2609 2610 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 })2615+2616+ res = await app.request('/another-app/undefined')2617+ expect(res.status).toBe(404)2618+ expect(await res.text()).toBe('Not Found from App')2619 })2620 2621+ 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+ })2628+2629 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 lines2785 (content: string | Promise<string>, head: { title: string }): Response | Promise<Response>2786 }2787 }2788 2789+describe('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+})2805+2806+describe('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))2813+2814+ 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+})2821+2822 describe('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