📖 Scenario: You are building a simple NestJS service that calculates discounts for customers. To make sure your service works correctly every time, you will write tests. This helps catch mistakes early and keeps your app reliable.
🎯 Goal: Create a NestJS service with a method to calculate discounts and write a test to verify it works as expected.
📋 What You'll Learn
Create a NestJS service class named DiscountService
Add a method calculateDiscount that takes a price number and returns a 10% discount
Create a test file with a test case that checks if calculateDiscount returns the correct discounted price
💡 Why This Matters
🌍 Real World
In real apps, testing services ensures that business logic works correctly before users see it, preventing bugs and improving trust.
💼 Career
Writing tests and using NestJS testing tools are key skills for backend developers to deliver reliable and maintainable applications.
Progress0 / 4 steps
1
Create the DiscountService class
Create a NestJS service class called DiscountService with a method calculateDiscount that takes a parameter price and returns price without any changes.
NestJS
Hint
Start by defining a class and a method that returns the input price.
2
Add discount calculation logic
Modify the calculateDiscount method in DiscountService to return 90% of the price (apply a 10% discount).
NestJS
Hint
Multiply the price by 0.9 to apply a 10% discount.
3
Set up a test for DiscountService
Create a test file that imports DiscountService and write a test case named 'should return 90% of the price' that checks if calculateDiscount(100) returns 90.
NestJS
Hint
Use describe and it blocks with expect to write the test.
4
Complete the test setup with NestJS testing module
Modify the test to use NestJS Test.createTestingModule to provide DiscountService and get the service instance from the module before running the test.
NestJS
Hint
Use NestJS testing utilities to create a testing module and get the service instance.
Practice
(1/5)
1. Why is testing important in a NestJS application?
easy
A. It reduces the size of the application
B. It makes the app run faster
C. It automatically writes code for you
D. It helps find bugs before users encounter them
Solution
Step 1: Understand the purpose of testing
Testing is used to check if the application works correctly and to find errors early.
Step 2: Identify the benefit of testing in NestJS
Testing helps catch bugs before users see them, improving reliability.
Final Answer:
It helps find bugs before users encounter them -> Option D
Quick Check:
Testing finds bugs early = C [OK]
Hint: Testing finds bugs early to keep apps reliable [OK]
Common Mistakes:
Thinking testing makes app faster
Confusing testing with code generation
Believing testing reduces app size
2. Which of the following is the correct way to define a simple test in NestJS using Jest?
easy
A. run('should return true', () => { expect(true).toBe(true); });
B. check('should return true', () => { expect(true).toBe(true); });
C. test('should return true', () => { expect(true).toBe(true); });
D. assert('should return true', () => { expect(true).toBe(true); });
Solution
Step 1: Recall Jest test syntax in NestJS
Jest uses the test function to define tests.
Step 2: Match the correct function name
Only test is the valid Jest function; others are invalid.