nestjs+jest测试框架学习记录一

1、安装所需的 npm 包:
$ npm i --save-dev @nestjs/testing

@nestjs/testing提供了一套提升测试过程的实用工具。

2、nestjs框架集成了jest测试框架,测试文件必须以 .spec 或 .test 结尾,测试文件位于与src同级的test目录下,如下图所示:
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 " 的 . s p e c . t s 更 改 为 想 要 测 试 的 测 试 单 元 文 件 。 此 处 我 要 测 试 u s e r s . c o n t r o l l e r , 所 以 更 改 为 u s e r s . c o n t r o l l e r . s p e c . t s "的.spec.ts更改为想要测试的测试单元文件。此处我要测试users.controller,所以更改为users.controller.spec.ts ".spec.tsusers.controllerusers.controller.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();
});

运行成功,优雅退出,结果如下图:
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值