-
-
Notifications
You must be signed in to change notification settings - Fork 700
/
Copy pathindex.test.ts
42 lines (37 loc) · 1.57 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Hono } from '../../hono'
import { testClient } from '.'
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 () => {
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 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()
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()
})
})