2.0.0

Midway Hooks 提供了单元测试工具来帮你完成单测。

请安装:@midwayjs/hooks-testing-library

测试规范

测试文件需满足以下任一条件:

  • 测试文件在 /src/apis 中
  • 测试文件以 .server.test.ts 结尾

API

createApp

创建用于测试的 Application

  1. import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
  2. describe('test new features', () => {
  3. let app: HooksApplication
  4. beforeAll(async () => {
  5. app = await createApp()
  6. })
  7. afterAll(async () => {
  8. await app.close()
  9. })
  10. })

createFunctionApp

用法同 createApp,用于测试 Midway FaaS 应用

  1. import {
  2. createFunctionApp,
  3. HooksApplication,
  4. } from '@midwayjs/hooks-testing-library'
  5. describe('test new features', () => {
  6. let app: HooksApplication
  7. beforeAll(async () => {
  8. app = await createFunctionApp()
  9. })
  10. afterAll(async () => {
  11. await app.close()
  12. })
  13. })

app.runFunction

通过 runFunction 来便捷调用函数并测试

  1. import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
  2. import api, { get, post } from '.'
  3. describe('test new features', () => {
  4. let app: HooksApplication
  5. beforeAll(async () => {
  6. app = await createApp()
  7. })
  8. it('runFunction', async () => {
  9. expect(await app.runFunction(api)).toMatchInlineSnapshot(`
  10. Object {
  11. "message": "Hello World",
  12. "method": "GET",
  13. }
  14. `)
  15. expect(await app.runFunction(post, 'Jake')).toMatchInlineSnapshot(
  16. `"postJake"`
  17. )
  18. })
  19. afterAll(async () => {
  20. await app.close()
  21. })
  22. })

app.request

通过 app.request 来运行完整的 http 单元测试。

  1. import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
  2. import api, { get, post } from '.'
  3. describe('test new features', () => {
  4. let app: HooksApplication
  5. beforeAll(async () => {
  6. app = await createApp()
  7. })
  8. afterAll(async () => {
  9. await app.close()
  10. })
  11. it('request', async () => {
  12. const response = await app.request(get).expect(200)
  13. expect(response.text).toMatchInlineSnapshot(`"get"`)
  14. })
  15. })

app.close

关闭创建的 App

  1. import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
  2. import api, { get, post } from '.'
  3. describe('test new features', () => {
  4. let app: HooksApplication
  5. beforeAll(async () => {
  6. app = await createApp()
  7. })
  8. afterAll(async () => {
  9. await app.close()
  10. })
  11. })

完整单测例子

接口 src/apis/index.ts

  1. import { useContext } from '@midwayjs/hooks'
  2. function useKoaContext() {
  3. return useContext()
  4. }
  5. export default async () => {
  6. return {
  7. message: 'Hello World',
  8. method: useKoaContext().method,
  9. }
  10. }
  11. export const get = async () => {
  12. return 'get'
  13. }
  14. export const post = async (name: string) => {
  15. return 'post' + name
  16. }

测试 src/apis/index.test.ts

  1. import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
  2. import api, { get, post } from '.'
  3. describe('test new features', () => {
  4. let app: HooksApplication
  5. beforeAll(async () => {
  6. app = await createApp()
  7. })
  8. afterAll(async () => {
  9. await app.close()
  10. })
  11. it('runFunction', async () => {
  12. expect(await app.runFunction(api)).toMatchInlineSnapshot(`
  13. Object {
  14. "message": "Hello World",
  15. "method": "GET",
  16. }
  17. `)
  18. expect(await app.runFunction(post, 'Jake')).toMatchInlineSnapshot(
  19. `"postJake"`
  20. )
  21. })
  22. it('request', async () => {
  23. const response = await app.request(get).expect(200)
  24. expect(response.text).toMatchInlineSnapshot(`"get"`)
  25. })
  26. })