Generate NestJS Unit Tests
Create comprehensive unit tests for NestJS services and controllers using Jest.
Important: Follow the Learning Mode guidelines in
_templates/learning-mode.md
Arguments
$ARGUMENTS- File path to service/controller (e.g., "users.service.ts")
Instructions
When the user runs /nest-test <file>:
Step 1: Analyze the file
Read the target file and identify:
- Class name and type (Service/Controller)
- Dependencies (injected services)
- Methods to test
- External calls (Prisma, other services)
Step 2: Show test plan
📋 Test Plan for: UsersService
Methods to test:
1. findAll() - List users
2. findOne(id) - Get single user
3. create(dto) - Create user
4. update(id, dto) - Update user
5. remove(id) - Delete user
Mocks needed:
- PrismaService
- EmailService (if used)
Test file: users.service.spec.ts
Ask: "Bạn có muốn thêm/bớt test cases nào không?"
Step 3: Create test file
Location: Same folder as source file
Naming: <filename>.spec.ts
Step 4: Write tests step by step
For EACH method, write:
- Happy path - Normal successful case
- Edge cases - Empty data, null values
- Error cases - Not found, validation errors
Test structure:
describe('UsersService', () => {
let service: UsersService;
let prisma: PrismaService;
beforeEach(async () => {
// Setup TestingModule with mocks
});
describe('findOne', () => {
it('should return a user when found', async () => {
// Arrange
// Act
// Assert
});
it('should throw NotFoundException when user not found', async () => {
// Test error case
});
});
});
Step 5: Explain after each test
- Explain the AAA pattern (Arrange-Act-Assert)
- Explain WHY we mock dependencies
- Explain the assertion used
- Ask: "Bạn hiểu test này chưa?"
Testing Best Practices
AAA Pattern:
// Arrange - Setup data and mocks const mockUser = { id: '1', email: 'test@test.com' }; prisma.user.findUnique.mockResolvedValue(mockUser); // Act - Call the method const result = await service.findOne('1'); // Assert - Check results expect(result).toEqual(mockUser);Mocking Prisma:
const mockPrisma = { user: { findUnique: jest.fn(), findMany: jest.fn(), create: jest.fn(), update: jest.fn(), delete: jest.fn(), }, };Testing exceptions:
it('should throw NotFoundException', async () => { prisma.user.findUnique.mockResolvedValue(null); await expect(service.findOne('999')) .rejects.toThrow(NotFoundException); });Test isolation:
- Each test should be independent
- Use
beforeEachto reset mocks - Don't share state between tests
Example Usage
/nest-test backend/src/modules/users/users.service.ts
/nest-test backend/src/modules/auth/auth.controller.ts
Run Tests
After creating tests:
# Run specific test file
pnpm run test users.service.spec.ts
# Run with coverage
pnpm run test:cov
After Completion
Remind user:
- "Nhớ update TRACKPAD.md với testing patterns học được!"
- Suggest: "Chạy
pnpm run testđể verify tests pass" - Link to Jest docs if needed
Converted and distributed by TomeVault — claim your Tome and manage your conversions.