Skip to content

测试

¥Testing

测试很重要。实际上,测试 Hono 的应用很容易。创建测试环境的方式因每个运行时而异,但基本步骤相同。在本节中,让我们使用 Cloudflare Workers 和 Jest 进行测试。

¥Testing is important. In actuality, it is easy to test Hono's applications. The way to create a test environment differs from each runtime, but the basic steps are the same. In this section, let's test with Cloudflare Workers and Jest.

请求和响应

¥Request and Response

你所要做的就是创建一个请求并将其传递给 Hono 应用以验证响应。你可以使用 app.request 的有用方法。

¥All you do is create a Request and pass it to the Hono application to validate the Response. And, you can use app.request the useful method.

提示

对于类型测试客户端,请参阅 测试助手

¥For a typed test client see the testing helper.

例如,考虑一个提供以下 REST API 的应用。

¥For example, consider an application that provides the following REST API.

ts
app.get('/posts', (c) => {
  return c.text('Many posts')
})

app.post('/posts', (c) => {
  return c.json(
    {
      message: 'Created',
    },
    201,
    {
      'X-Custom': 'Thank you',
    }
  )
})

GET /posts 发出请求并测试响应。

¥Make a request to GET /posts and test the response.

ts
describe('Example', () => {
  test('GET /posts', async () => {
    const res = await app.request('/posts')
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('Many posts')
  })
})

要向 POST /posts 发出请求,请执行以下操作。

¥To make a request to POST /posts, do the following.

ts
test('POST /posts', async () => {
  const res = await app.request('/posts', {
    method: 'POST',
  })
  expect(res.status).toBe(201)
  expect(res.headers.get('X-Custom')).toBe('Thank you')
  expect(await res.json()).toEqual({
    message: 'Created',
  })
})

要使用 JSON 数据向 POST /posts 发出请求,请执行以下操作。

¥To make a request to POST /posts with JSON data, do the following.

ts
test('POST /posts', async () => {
  const res = await app.request('/posts', {
    method: 'POST',
    body: JSON.stringify({ message: 'hello hono' }),
    headers: new Headers({ 'Content-Type': 'application/json' }),
  })
  expect(res.status).toBe(201)
  expect(res.headers.get('X-Custom')).toBe('Thank you')
  expect(await res.json()).toEqual({
    message: 'Created',
  })
})

要使用 multipart/form-data 数据向 POST /posts 发出请求,请执行以下操作。

¥To make a request to POST /posts with multipart/form-data data, do the following.

ts
test('POST /posts', async () => {
  const formData = new FormData()
  formData.append('message', 'hello')
  const res = await app.request('/posts', {
    method: 'POST',
    body: formData,
  })
  expect(res.status).toBe(201)
  expect(res.headers.get('X-Custom')).toBe('Thank you')
  expect(await res.json()).toEqual({
    message: 'Created',
  })
})

你还可以传递 Request 类的实例。

¥You can also pass an instance of the Request class.

ts
test('POST /posts', async () => {
  const req = new Request('http://localhost/posts', {
    method: 'POST',
  })
  const res = await app.request(req)
  expect(res.status).toBe(201)
  expect(res.headers.get('X-Custom')).toBe('Thank you')
  expect(await res.json()).toEqual({
    message: 'Created',
  })
})

通过这种方式,你可以像端到端一样对其进行测试。

¥In this way, you can test it as like an End-to-End.

Env

要设置 c.env 进行测试,你可以将其作为第 3 个参数传递给 app.request。这对于模拟 Cloudflare Workers Bindings 之类的值很有用:

¥To set c.env for testing, you can pass it as the 3rd parameter to app.request. This is useful for mocking values like Cloudflare Workers Bindings:

ts
const MOCK_ENV = {
  API_HOST: 'example.com',
  DB: {
    prepare: () => {
      /* mocked D1 */
    },
  },
}

test('GET /posts', async () => {
  const res = await app.request('/posts', {}, MOCK_ENV)
})

Hono v4.7 中文网 - 粤ICP备13048890号