Launch Week Day 1: Socket for Jira Is Now Available.Learn More
Socket
Book a DemoSign in
Socket

@golevelup/ts-sinon

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@golevelup/ts-sinon

Utilities for making testing [NestJS](https://docs.nestjs.com) applications easier.

latest
Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
17K
56.61%
Maintainers
3
Weekly downloads
 
Created
Source

Sinon Mocking (ts-sinon)

Utilities for making testing NestJS applications easier.

Getting Started

::: code-group

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

:::

Creating Mocks

  • Import the createMock function into your test class.
  • Create a variable and set it equal to the createMock function with its generic type input.
  • 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. :::

Keywords

NestJS

FAQs

Package last updated on 18 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts