Skip to content
Back to skills

Nestjs Testing Patterns

ASecurity

Guides writing isolated unit tests and integration tests for NestJS controllers, services, and guards using Test.createTestingModule() and Vitest/Jest.

  • 6 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 7, 2026
ai-agentstypescripttestingapi

Works with

  • api

Security analysis

A100/100

Scanned September 7, 2026

npx -y skills add smicolon/ai-kit --skill nestjs-testing-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Nestjs Testing Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Nestjs Testing Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/smicolon-nestjs-testing-patterns/badge)](https://www.skillsdirectory.com/skills/smicolon-nestjs-testing-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nestjs-testing-patterns
description: Guides writing isolated unit tests and integration tests for NestJS controllers, services, and guards using Test.createTestingModule() and Vitest/Jest.
version: 1.0.0
---

# NestJS Testing Patterns

Best practices for writing unit and integration tests in NestJS applications.

## 1. Service Unit Testing (Mocked Dependencies)

Test business logic in isolation by overriding providers with mocks:

```typescript
import { Test, TestingModule } from '@nestjs/testing'
import { UsersService } from './users.service'
import { getRepositoryToken } from '@nestjs/typeorm'
import { User } from '../entities'

describe('UsersService', () => {
  let service: UsersService
  const mockUserRepository = {
    find: jest.fn(),
    findOneBy: jest.fn(),
    save: jest.fn(),
  }

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UsersService,
        {
          provide: getRepositoryToken(User),
          useValue: mockUserRepository,
        },
      ],
    }).compile()

    service = module.get<UsersService>(UsersService)
  })

  it('should find user by id', async () => {
    const expectedUser = { id: 'uuid-1', email: 'test@example.com' }
    mockUserRepository.findOneBy.mockResolvedValue(expectedUser)

    const result = await service.findById('uuid-1')
    expect(result).toEqual(expectedUser)
    expect(mockUserRepository.findOneBy).toHaveBeenCalledWith({ id: 'uuid-1' })
  })
})
```

---

## 2. Controller Unit Testing

```typescript
import { Test, TestingModule } from '@nestjs/testing'
import { UsersController } from './users.controller'
import { UsersService } from './users.service'

describe('UsersController', () => {
  let controller: UsersController
  const mockUsersService = {
    findAll: jest.fn().mockResolvedValue([]),
  }

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [UsersController],
      providers: [
        {
          provide: UsersService,
          useValue: mockUsersService,
        },
      ],
    }).compile()

    controller = module.get<UsersController>(UsersController)
  })

  it('should return an array of users', async () => {
    expect(await controller.findAll()).toEqual([])
  })
})
```

---

## 3. End-to-End (e2e) Integration Testing

```typescript
import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication, ValidationPipe } from '@nestjs/common'
import * as request from 'supertest'
import { AppModule } from '../src/app.module'

describe('App (e2e)', () => {
  let app: INestApplication

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile()

    app = moduleFixture.createNestApplication()
    app.useGlobalPipes(new ValidationPipe({ whitelist: true }))
    await app.init()
  })

  afterAll(async () => {
    await app.close()
  })

  it('/api/health (GET)', () => {
    return request(app.getHttpServer())
      .get('/api/health')
      .expect(200)
      .expect({ status: 'ok' })
  })
})
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.