@golevelup/nestjs-testing

中文文档
0.1.2发布于 2019/11/14MITTypeScriptCommonJS零依赖近 30 天下载 8.4万

Reusable utilties to help level up NestJS Testing

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录6 节
  1. Description
  2. Motivation
  3. Side Note
  4. Usage
  5. Installation
  6. Creating Mocks

@golevelup/nestjs-testing

version downloads license

Description

Utilities for making testing NestJS applications easier. Currently supports Jest.

Motivation

You ever just come across a type that you want to mock, but mocking the entire object seems daunting, and who knows how many sub properties the object has and if those sub properties have sub properties. The list goes on with possible problems. Enter @golevelup/nestjs-testing's createMock utility function. This function will create a mock object for you with all sub properties mocked as jest.fn() unless otherwise provided, to allow for easy mocking later on, but more on that later.

Side Note

This package and utility function was derived out of a want to help those unit testing things like Guards, Interceptors, and Filters, in NestJS; however, given the dynamic nature of the package, the createMock utility function can handle so much more than just what's inside of NestJS. Essentially, if it has an interface, @golevelup/nestjs-testing can mock it!

Usage

For examples we'll show how well it works with NestJS' ExecutionContext which extends NestJS' ArgumentHost.

Installation

Pretty standard installation, nothing too crazy

npm i @golevelup/nestjs-testing

or

yarn @golevelup/nestjs-testing

Creating Mocks

Here is where the fun begins. As a heads up, this function does require you to be using Typescript, as it takes advantage of some advanced Typescript features under the hood, like Proxies.

  1. Import the createMock function into your test class
  2. Create a variable and set it equal to the createMock function plus its generic type input
  3. Use the mock, Luke.

Example:

import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';

describe('Mocked Execution Context', () => {
  it('should have a fully mocked Execution Context', () => {
    const mockExecutionContext = createMock<ExecutionContext>();
    expect(mockExecutionContext.switchToHttp()).toBeDefined();
  });
});

And just like that, the simplest mock can quickly and easily be made! "Big deal" you may say? It's easy to create a mock with a single property like that? Well, just watch this:

import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';

describe('Mocked Execution Context', () => {
  it('should have a fully mocked Execution Context', () => {
    const mockExecutionContext = createMock<ExecutionContext>();
    expect(mockExecutionContext.switchToHttp().getRequest()).toBeDefined();
    expect(mockExecutionContext.switchToRPC().getContext()).toBeDefined();
    expect(mockExecutionContext.switchToWs().getClient()).toBeDefined();
  });
});

How's that for ya? No, still not impressed? All right, time to bring out the big guns.

import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';

describe('Mocked Execution Context', () => {
  it('should have a fully mocked Execution Context', () => {
    const mockExecutionContext = createMock<ExecutionContext>({
      switchToHttp: () => ({
        getRequest: () =({
          headers: {
            authorization: 'auth'
          }
        })
      })
    });
    mockExecutionContext.switchToHttp().getResponse.mockReturnValue({data: 'res return data'});
    expect(mockExecutionContext.switchToHttp().getRequest()).toEqual({
      headers: {
        authorization: 'auth'
      }
    });
    expect(mockExecutionContext.switchToHttp().getResponse()).toEqual({
      data: 'res return data'
    });
    expect(mockExecutionContext.switchToHttp).toBeCalledTimes(3);
    expect(mockExecutionContext.switchToRPC().getContext()).toBeDefined();
    expect(mockExecutionContext.switchToWs().getClient()).toBeDefined();
  });
});

Note: Be aware that when providing your own mocks, if you asserting how many times you called a parent mock function, the number will be equal to the number of times the function was called in your expects plus the number of times the function had to be called to set your mocks. In the above case, we had to call switchToHttp() once to set the mock for getResponse() and twice for the expect calls, so it was called in a total of three times.

The above case shows how well the createMock utility can take in user provided values as well as returning type safe mocks that can easily be chained and modified as needed.

For a few more examples on what can be done the mock.spec file has some really cool examples that show pretty well just what is doable with this utility.

版本与发布记录

@golevelup/nestjs-testing 共发布 3 个版本,首次发布于 2019/11/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2019/11/147 年前
0.1.12019/11/107 年前
0.1.02019/11/097 年前

关于 @golevelup/nestjs-testing 的常见问题

@golevelup/nestjs-testing 是什么?

Reusable utilties to help level up NestJS Testing注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @golevelup/nestjs-testing?

在项目目录运行 npm install @golevelup/nestjs-testing,或使用 pnpm add / yarn add / bun add。

@golevelup/nestjs-testing 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@golevelup/nestjs-testing 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@golevelup/nestjs-testing 可以商用吗?

@golevelup/nestjs-testing 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@golevelup/nestjs-testing 的下载量有多少?

根据 npm 官方下载统计,@golevelup/nestjs-testing 在 2026/10/17 – 2026/11/15 这 30 天内被下载 83,981 次,日均约 2,799 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@golevelup/nestjs-testing 还在维护吗?

最新版本 0.1.2 发布于 2019/11/14(7 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/09。作者已将其标记为弃用,新项目建议寻找替代方案。