Skip to content

Commit

Permalink
fix(testing): set baseUrl for testClient (#2496)
Browse files Browse the repository at this point in the history
* fix(testing): set `baseUrl` for `testClient`

* denoify
  • Loading branch information
yusukebe committed Apr 11, 2024
1 parent 0a92236 commit f5104e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno_dist/helper/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const testClient = <T extends Hono<any, any, any>>(
return app.request(input, init, Env, executionCtx)
}

return hc<typeof app>('', { fetch: customFetch })
return hc<typeof app>('http://localhost', { fetch: customFetch })
}
19 changes: 16 additions & 3 deletions src/helper/testing/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { Hono } from '../../hono'
import { testClient } from '.'

describe('hono testClinet', () => {
it('should return the correct search result', async () => {
describe('hono testClient', () => {
it('Should return the correct search result', async () => {
const app = new Hono().get('/search', (c) => c.json({ hello: 'world' }))
const res = await testClient(app).search.$get()
expect(await res.json()).toEqual({ hello: 'world' })
})

it('should return the correct environment variables value', async () => {
it('Should return the correct environment variables value', async () => {
type Bindings = { hello: string }
const app = new Hono<{ Bindings: Bindings }>().get('/search', (c) => {
return c.json({ hello: c.env.hello })
})
const res = await testClient(app, { hello: 'world' }).search.$get()
expect(await res.json()).toEqual({ hello: 'world' })
})

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()
expect(url.pathname).toBe('/abc')
})

it('Should not throw an error with $ws()', async () => {
vi.stubGlobal('WebSocket', class {})
const app = new Hono().get('/ws', (c) => c.text('Fake response of a WebSocket'))
// @ts-expect-error $ws is not typed correctly
expect(() => testClient(app).ws.$ws()).not.toThrowError()
})
})
2 changes: 1 addition & 1 deletion src/helper/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const testClient = <T extends Hono<any, any, any>>(
return app.request(input, init, Env, executionCtx)
}

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

0 comments on commit f5104e8

Please sign in to comment.