2.0.0
Midway Hooks 提供了单元测试工具来帮你完成单测。
请安装:@midwayjs/hooks-testing-library
测试规范
测试文件需满足以下任一条件:
- 测试文件在 /src/apis 中
- 测试文件以 .server.test.ts 结尾
API
createApp
创建用于测试的 Application
import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createApp()
})
afterAll(async () => {
await app.close()
})
})
createFunctionApp
用法同 createApp,用于测试 Midway FaaS 应用
import {
createFunctionApp,
HooksApplication,
} from '@midwayjs/hooks-testing-library'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createFunctionApp()
})
afterAll(async () => {
await app.close()
})
})
app.runFunction
通过 runFunction
来便捷调用函数并测试
import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
import api, { get, post } from '.'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createApp()
})
it('runFunction', async () => {
expect(await app.runFunction(api)).toMatchInlineSnapshot(`
Object {
"message": "Hello World",
"method": "GET",
}
`)
expect(await app.runFunction(post, 'Jake')).toMatchInlineSnapshot(
`"postJake"`
)
})
afterAll(async () => {
await app.close()
})
})
app.request
通过 app.request
来运行完整的 http 单元测试。
import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
import api, { get, post } from '.'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createApp()
})
afterAll(async () => {
await app.close()
})
it('request', async () => {
const response = await app.request(get).expect(200)
expect(response.text).toMatchInlineSnapshot(`"get"`)
})
})
app.close
关闭创建的 App
import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
import api, { get, post } from '.'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createApp()
})
afterAll(async () => {
await app.close()
})
})
完整单测例子
接口 src/apis/index.ts
import { useContext } from '@midwayjs/hooks'
function useKoaContext() {
return useContext()
}
export default async () => {
return {
message: 'Hello World',
method: useKoaContext().method,
}
}
export const get = async () => {
return 'get'
}
export const post = async (name: string) => {
return 'post' + name
}
测试 src/apis/index.test.ts
import { createApp, HooksApplication } from '@midwayjs/hooks-testing-library'
import api, { get, post } from '.'
describe('test new features', () => {
let app: HooksApplication
beforeAll(async () => {
app = await createApp()
})
afterAll(async () => {
await app.close()
})
it('runFunction', async () => {
expect(await app.runFunction(api)).toMatchInlineSnapshot(`
Object {
"message": "Hello World",
"method": "GET",
}
`)
expect(await app.runFunction(post, 'Jake')).toMatchInlineSnapshot(
`"postJake"`
)
})
it('request', async () => {
const response = await app.request(get).expect(200)
expect(response.text).toMatchInlineSnapshot(`"get"`)
})
})