Skip to content

Commit

Permalink
test(hono-base): add tests for covering 100% (#2952)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jun 9, 2024
1 parent 7eb3e3b commit e1e0e09
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,10 @@ describe('Error handle', () => {
throw new Error('This is Error')
})

app.get('/error-string', () => {
throw 'This is Error'
})

app.use('/error-middleware', async () => {
throw new Error('This is Middleware Error')
})
Expand All @@ -1397,6 +1401,10 @@ describe('Error handle', () => {
return c.text('Custom Error Message', 500)
})

it('Should throw Error if a non-Error object is thrown in a handler', async () => {
expect(() => app.request('/error-string')).toThrowError()
})

it('Custom Error Message', async () => {
let res = await app.request('https://example.com/error')
expect(res.status).toBe(500)
Expand Down Expand Up @@ -2505,7 +2513,7 @@ describe('Optional parameters', () => {

describe('app.mount()', () => {
describe('Basic', () => {
const anotherApp = (req: Request, params: unknown) => {
const anotherApp = (req: Request, ...params: unknown[]) => {
const path = getPath(req)
if (path === '/') {
return new Response('AnotherApp')
Expand Down Expand Up @@ -2533,6 +2541,9 @@ describe('app.mount()', () => {
}
)
}
if (path === '/undefined') {
return undefined as unknown as Response
}
return new Response('Not Found from AnotherApp', {
status: 404,
})
Expand All @@ -2544,9 +2555,16 @@ describe('app.mount()', () => {
c.header('x-message', 'Foo')
})
app.get('/', (c) => c.text('Hono'))
app.notFound((c) => {
return c.text('Not Found from App', 404)
})

app.mount('/another-app', anotherApp, () => {
return 'params'
})
app.mount('/another-app-with-array-option', anotherApp, () => {
return ['param1', 'param2']
})
app.mount('/another-app2/sub-slash/', anotherApp)

const api = new Hono().basePath('/api')
Expand Down Expand Up @@ -2592,7 +2610,19 @@ describe('app.mount()', () => {
res = await app.request('/another-app/with-params')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
params: 'params',
params: ['params'],
})

res = await app.request('/another-app/undefined')
expect(res.status).toBe(404)
expect(await res.text()).toBe('Not Found from App')
})

it('Should return response from Another app with an array option', async () => {
const res = await app.request('/another-app-with-array-option/with-params')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
params: ['param1', 'param2'],
})
})

Expand Down Expand Up @@ -2756,6 +2786,39 @@ declare module './context' {
}
}

describe('app.request()', () => {
it('Should return response with Request and RequestInit as args', async () => {
const app = new Hono()
app.get('/foo', (c) => {
return c.json(c.req.header('x-message'))
})
const req = new Request('http://localhost/foo')
const headers = new Headers()
headers.append('x-message', 'hello')
const res = await app.request(req, {
headers,
})
expect(res.status).toBe(200)
expect(await res.text()).toBe('"hello"')
})
})

describe('app.fire()', () => {
it('Should call global.addEventListener', () => {
const app = new Hono()
const addEventListener = vi.fn()
global.addEventListener = addEventListener
app.fire()
expect(addEventListener).toHaveBeenCalledWith('fetch', expect.any(Function))

const fetchEventListener = addEventListener.mock.calls[0][1]
const respondWith = vi.fn()
const request = new Request('http://localhost')
fetchEventListener({ respondWith, request })
expect(respondWith).toHaveBeenCalledWith(expect.any(Promise))
})
})

describe('Context render and setRenderer', () => {
const app = new Hono()
app.get('/default', (c) => {
Expand Down

0 comments on commit e1e0e09

Please sign in to comment.