📖 Scenario: You are building a simple NestJS service that manages a list of books. You want to make sure your service works correctly by writing integration tests that check the full flow of your application.
🎯 Goal: Build a NestJS integration test that sets up the testing module, injects the service, and verifies that the service returns the expected list of books.
📋 What You'll Learn
Create a basic array of book objects as initial data
Set up a NestJS testing module with the service
Write an integration test that calls the service method
Assert the returned data matches the initial book list
💡 Why This Matters
🌍 Real World
Integration tests help ensure that different parts of your NestJS application work together correctly before deploying to users.
💼 Career
Writing integration tests is a key skill for backend developers to maintain reliable and bug-free applications in professional environments.
Progress0 / 4 steps
1
DATA SETUP: Create initial book data
Create a constant array called books with these exact objects: { id: 1, title: 'NestJS Basics' } and { id: 2, title: 'Testing with Jest' }.
NestJS
Hint
Use const books = [ ... ] with two objects inside.
2
CONFIGURATION: Set up NestJS testing module
Create a variable called moduleRef and assign it the result of await Test.createTestingModule({ providers: [BooksService] }).compile() inside an async function.
NestJS
Hint
Use await Test.createTestingModule({ providers: [BooksService] }).compile() and assign it to moduleRef.
3
CORE LOGIC: Retrieve service and call method
Create a variable called booksService and assign it the result of moduleRef.get(BooksService). Then call booksService.findAll() and assign the result to result.
NestJS
Hint
Use moduleRef.get(BooksService) to get the service, then call findAll().
4
COMPLETION: Assert the returned books match initial data
Write an assertion using expect(result).toEqual(books) to check that the service returns the correct book list.
NestJS
Hint
Use expect(result).toEqual(books) to check the returned data.
Practice
(1/5)
1. What is the main purpose of integration testing in NestJS?
easy
A. To test individual functions in isolation
B. To check how different parts of the application work together
C. To measure application performance under load
D. To check the syntax of TypeScript code
Solution
Step 1: Understand integration testing goal
Integration testing focuses on verifying that multiple parts of the app interact correctly.
Step 2: Differentiate from other test types
Unit tests check single functions; performance tests measure speed; syntax checks are done by compilers.
Final Answer:
To check how different parts of the application work together -> Option B
Quick Check:
Integration testing = check parts working together [OK]
Hint: Integration tests check combined parts, not single functions [OK]
Common Mistakes:
Confusing integration with unit testing
Thinking integration tests check performance
Believing integration tests check code syntax
2. Which of the following is the correct way to initialize a NestJS application for integration testing?
easy
A. const app = await Test.createTestingModule({ imports: [AppModule] }).start();
B. const app = new AppModule(); app.start();
C. const app = await Test.createTestingModule({ controllers: [AppController] }).init();