Skip to content

测试

🌐 Testing

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

🌐 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 Vitest.

TIP

Cloudflare 建议将 Vitest@cloudflare/vitest-pool-workers 一起使用。有关更多详细信息,请参阅 Cloudflare Workers 文档中的 Vitest 集成

请求和响应

🌐 Request and Response

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

🌐 All you need to do is create a Request and pass it to the Hono application to validate the Response. You can then use the useful app.request method.

TIP

对于一个类型化的测试客户端,请参阅 测试助手

例如,考虑一个提供以下 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 绑定 这样的值很有用:

🌐 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 中文网 - 粤ICP备13048890号