@golevelup/ts-sinon

中文文档
2.0.0发布于 2026/03/19MITTypeScriptCommonJS零依赖近 30 天下载 11.4万

Utilities for making testing NestJS applications easier.

原始使用文档

Sinon Mocking (ts-sinon)

Utilities for making testing NestJS applications easier.

version downloads license

Getting Started

::: code-group

npm install @golevelup/ts-sinon -D
yarn add @golevelup/ts-sinon -D
pnpm add @golevelup/ts-sinon -D

:::

Creating Mocks

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

Here's an example with NestJS' ExecutionContext:

import { createMock } from '@golevelup/ts-sinon';
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();
  });
});

createMock generates all sub-properties as sinon.stub(), so you can chain method calls:

it('should correctly resolve mocked providers', async () => {
  const request = {
    key: 'val',
  };

  mockExecutionContext.switchToHttp.returns(
    createMock<HttpArgumentsHost>({
      getRequest: () => request,
    }),
  );

  const mockResult = mockExecutionContext.switchToHttp().getRequest();
  expect(mockResult).toBe(request);
});

You can also easily provide your own mocks:

const mockExecutionContext = createMock<ExecutionContext>({
  switchToHttp: () => ({
    getRequest: () => ({
      headers: {
        authorization: 'auth',
      },
    }),
    getResponse: sinon.stub().returns({ data: 'res return data' }),
  }),
});

::: warning Note When providing your own mocks, the number of times a parent mock function was called includes the times needed to set your mocks. :::

版本与发布记录

@golevelup/ts-sinon 共发布 9 个版本,首次发布于 2023/07/18;过去 12 个月发布了 5 个版本。

版本发布日期距今
2.0.0latest2026/03/197 个月前
1.1.22026/03/178 个月前
1.1.12025/12/1910 个月前
1.1.02025/12/1910 个月前
1.0.02025/12/1910 个月前
0.1.32025/05/081.5 年前

关于 @golevelup/ts-sinon 的常见问题

@golevelup/ts-sinon 是什么?

Utilities for making testing NestJS applications easier.

如何安装 @golevelup/ts-sinon?

在项目目录运行 npm install @golevelup/ts-sinon,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:sinon(^21.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@golevelup/ts-sinon 支持 TypeScript 吗?

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

@golevelup/ts-sinon 支持 ESM 还是 CommonJS?

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

@golevelup/ts-sinon 可以商用吗?

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

@golevelup/ts-sinon 的下载量有多少?

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

@golevelup/ts-sinon 还在维护吗?

最新版本 2.0.0 发布于 2026/03/19(7 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/07/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。