1、安装所需的 npm 包:
$ npm i --save-dev @nestjs/testing
@nestjs/testing提供了一套提升测试过程的实用工具。
2、nestjs框架集成了jest测试框架,测试文件必须以 .spec 或 .test 结尾,测试文件位于与src同级的test目录下,如下图所示:
其中app.e2e-spec.ts文件是实现当前程序的端到端(end to end)测试,也可视为系统测试;jest-e2e.json为jest配置文件。3、实现单元测试
可以自己编写相应模块,包括controller和service提供程序,针对未依赖数据库的nest框架,官网提供了一个test demo,即位于src目录下的app.controller.spec.ts文件。
demo如下:
// 未依赖数据库
import { Test } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
describe('CatsController', () => {
let catsController: CatsController;
let catsService: CatsService;
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [CatsController],
providers: [CatsService],
}).compile();
catsService = module.get<CatsService>(CatsService);
catsController = module.get<CatsController>(CatsController);
});
describe('findAll', () => {
it('should return an array of cats', async () => {
const result = ['test'];
jest.spyOn(catsService, 'findAll').mockImplementation(() => result);
expect(await catsController.findAll()).toBe(result);
});
});
});
对于依赖于数据库的nest框架,需要在async 函数声明module时imports我们的数据库模块。否则会报错:Nest can’t resolve dependencies of the CoursesController (?). Please make sure that the argument CourseModel at index [0] is available in the RootTestModule context.
代码如图所示:
// 依赖数据库,数据库模块为DbModel
import { Test } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { DbModule } from '@libs/db';
describe('CatsController', () => {
let catsController: CatsController;
let catsService: CatsService;
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [CatsController],
providers: [CatsService],
imports:[DbModel],
}).compile();
catsService = module.get<CatsService>(CatsService);
catsController = module.get<CatsController>(CatsController);
});
describe('findAll', () => {
it('should return an array of cats', async () => {
const result = ['test'];
jest.spyOn(catsService, 'findAll').mockImplementation(() => result);
expect(await catsController.findAll()).toBe(result);
});
});
});
4、在终端输入:npm test或者npm run test启动测试,测试结果会显示在终端,如下图所示:
以上测试命令会运行整个项目中所有的测试文件,如果想要运行某个单一的测试单元,需要更改nest项目的package.json配置文件,原文件如下:
//
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
将"testRegex": ".spec.ts,如下图所示:
//
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testRegex": "users.controller.spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
在终端运行命令行:nest run test
结果如下图所示:
但是按照上述流程进行单元测试,遇到了如下问题:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren’t stopped in your tests. Consider running Jest with --detectOpenHandles
to troubleshoot this issue.
他的意思是jest在测试运行完成后一秒钟内没有退出,这通常意味着在测试中存在未停止的异步操作。考虑到我现在正在测试的这个单元需要连接数据库,所以错误原因可能是在测试单元没有关闭数据库。
解决方法是:
import * as mongoose from 'mongoose';
// 原代码,在it({});之后添加如下代码
afterAll(async () => {
mongoose.disconnect();
});
运行成功,优雅退出,结果如下图: