Skip to content

feat(hono/testing): Allow passing hc options to testClient #4059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/helper/testing/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe('hono testClient', () => {
expect(await res.json()).toEqual({ hello: 'world' })
})

it('Should use the passed in headers', async () => {
const app = new Hono().get('/search', (c) => {
return c.json({ query: c.req.header('x-query') })
})
const res = await testClient(app, undefined, undefined, {
headers: { 'x-query': 'abc' },
}).search.$get()
expect(await res.json()).toEqual({ query: 'abc' })
})

it('Should return a correct URL with out throwing an error', async () => {
const app = new Hono().get('/abc', (c) => c.json(0))
const url = testClient(app).abc.$url()
Expand Down
7 changes: 4 additions & 3 deletions src/helper/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { hc } from '../../client'
import type { Client } from '../../client/types'
import type { Client, ClientRequestOptions } from '../../client/types'
import type { ExecutionContext } from '../../context'
import type { Hono } from '../../hono'
import type { Schema } from '../../types'
Expand All @@ -16,11 +16,12 @@ type ExtractEnv<T> = T extends Hono<infer E, Schema, string> ? E : never
export const testClient = <T extends Hono<any, Schema, string>>(
app: T,
Env?: ExtractEnv<T>['Bindings'] | {},
executionCtx?: ExecutionContext
executionCtx?: ExecutionContext,
options?: Omit<ClientRequestOptions, 'fetch'>
): UnionToIntersection<Client<T>> => {
const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
return app.request(input, init, Env, executionCtx)
}

return hc<typeof app>('http://localhost', { fetch: customFetch })
return hc<typeof app>('http://localhost', { ...options, fetch: customFetch })
}
Loading