文档
质量
Unit-Test

单元测试

说明

使用

  1. 安装依赖
yarn add @yunflyjs/yunfly-unit-test --dev
  1. 项目根目录下新增jest.config.js文件,内容如下:
jest.config.js
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ["<rootDir>/src/__test__"],
  verbose: true,
};
    1. package.json 新增 test 脚本命令
package.json
"scripts": {
  "test": "jest -runInBand --forceExit --colors"
}

测试案例

简单案例

src/example.ts
// example.ts
export function sum(a:any, b:any) {
  return a + b;
}
  • 单词用例
src/__test__/http.simple.ts
// src/__test__/http.simple.ts
import { sum } from '../example.ts';
 
test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

更多能力请参考 jest (opens in a new tab)

测试接口案例

  • controller
src/controller/ExampleController.ts
import { Get, JsonController, BodyParam, Post, QueryParam } from '@yunflyjs/yunfly';
 
@JsonController('/example')
export default class ExampleController {
 
  @Get('/simple/get')
  async simple(
    @QueryParam('name') name: string,
  ): Promise<any> {
    return {
      name:name || 'success'
    };
  }
  
  @Post('/simple/post')
  simple1(
    @BodyParam('name') name: string,
  ): any {
    return {
      name:name || 'success'
    };
  }
}
  • 单测用例

  • get

src/__test__/http.get.ts
// src/__test__/http.get.ts
import { request } from '@yunflyjs/yunfly-unit-test'
 
test('GET /example/simple/get', async () => {
  const res = await request.get('/example/simple/get');
  expect(res.body).toEqual({ "name": "success" });
});
  • post
src/__test__/http.post.ts
// src/__test__/http.test.ts
import { request } from '@yunflyjs/yunfly-unit-test'
 
describe('POST /example/simple/post', function() {
  test('test default response', async () => {
    const res = await request
      .post('/example/simple/post')
      .set('Accept', 'application/json');
      expect(res.body.name).toEqual('success');
  });
 
  test('test request params', async () => {
    const res = await request
      .post('/example/simple/post')
      .send({name: 'john'})
      .set('Accept', 'application/json');
  
      expect(res.status).toEqual(200);
      expect(res.body.name).toEqual('john');
  });
})

request api 请参考 supertest (opens in a new tab)