📖 Scenario: You are building a simple Node.js module that calculates the total price of items in a shopping cart. To make sure your code works correctly and keeps working as you add features, you will write tests.
🎯 Goal: Build a Node.js module with a function to sum item prices and write a basic test to check it works correctly.
📋 What You'll Learn
Create a list of item prices as numbers
Create a variable to hold the total price starting at 0
Write a function that sums all prices in the list
Write a simple test using Node.js assert to check the function output
💡 Why This Matters
🌍 Real World
Testing helps catch mistakes early and keeps your code working as you add new features, just like checking your work in school before submitting.
💼 Career
Writing tests is a key skill for software developers to ensure code quality and reliability in real projects.
Progress0 / 4 steps
1
Create the list of item prices
Create a variable called prices and set it to an array with these exact numbers: 10, 20, 30
Node.js
Hint
Use const to create an array named prices with the numbers 10, 20, and 30 inside square brackets.
2
Create a variable to hold the total price
Create a variable called total and set it to 0 to start counting the sum of prices
Node.js
Hint
Use let to create a variable named total and set it to 0.
3
Write a function to sum all prices
Write a function called calculateTotal that takes the prices array and returns the sum of all prices using a for loop with price as the iterator variable
Node.js
Hint
Define a function named calculateTotal that loops over prices using for (const price of prices) and adds each price to a sum variable, then returns sum.
4
Write a simple test to check the function
Import Node.js assert module and write a test using assert.strictEqual to check that calculateTotal(prices) equals 60
Node.js
Hint
Use import assert from 'assert' at the top and then write assert.strictEqual(calculateTotal(prices), 60) to check the function returns 60.
Practice
(1/5)
1. Why is testing important in Node.js development?
easy
A. It helps find errors before users encounter them.
B. It makes the code run faster.
C. It automatically writes code for you.
D. It removes the need for documentation.
Solution
Step 1: Understand the purpose of testing
Testing is used to catch bugs and errors early in the development process.
Step 2: Compare options to testing benefits
Only It helps find errors before users encounter them. correctly states that testing finds errors before users see them, which is a key reason to test.
Final Answer:
It helps find errors before users encounter them. -> Option A
Quick Check:
Testing finds errors early = B [OK]
Hint: Testing finds bugs early to protect users [OK]
Common Mistakes:
Thinking testing speeds up code execution
Believing testing writes code automatically
Assuming testing replaces documentation
2. Which of the following is the correct syntax to define a simple test using Jest in Node.js?
easy
A. assert('adds numbers', () => { expect(1 + 1).toBe(2); });
B. test('adds numbers', () => { expect(1 + 1).toBe(2); });
C. runTest('adds numbers', () => { expect(1 + 1).toBe(2); });
D. check('adds numbers', () => { expect(1 + 1).toBe(2); });
Solution
Step 1: Recall Jest test syntax
Jest uses the function test() to define tests.
Step 2: Match syntax with options
Only test('adds numbers', () => { expect(1 + 1).toBe(2); }); uses test() correctly with a description and a function.
A. Missing assertion library usage like chai's expect
B. Incorrect describe function syntax
C. Throwing error inside test is not allowed
D. Array length check is wrong
Solution
Step 1: Review test structure and error handling
The test uses a manual if check and throws error instead of using assertion functions.
Step 2: Understand best practice for Mocha tests
Mocha tests usually use assertion libraries like Chai's expect or assert for clearer test results.
Final Answer:
Missing assertion library usage like chai's expect -> Option A
Quick Check:
Use assertions, not manual throw, in tests [OK]
Hint: Use assertion functions, not manual throw, in tests [OK]
Common Mistakes:
Thinking throwing error is standard test practice
Confusing describe syntax with error
Believing array length check is incorrect
5. You have a Node.js function that processes user data. After adding new features, some old tests fail. What is the best testing approach to keep your code trustworthy and easy to improve?
hard
A. Remove old tests to avoid failures.
B. Only test new features and ignore old tests.
C. Manually test the app without automated tests.
D. Write comprehensive automated tests and run them after every change.
Solution
Step 1: Understand the role of automated tests
Automated tests help catch regressions and ensure new changes don't break existing features.
Step 2: Evaluate options for maintaining code quality
Only Write comprehensive automated tests and run them after every change. promotes writing and running comprehensive automated tests regularly, which keeps code trustworthy and easier to improve.
Final Answer:
Write comprehensive automated tests and run them after every change. -> Option D
Quick Check:
Automated tests maintain trust and ease improvements [OK]
Hint: Run all tests after changes to catch issues early [OK]
Common Mistakes:
Ignoring old tests leads to hidden bugs
Relying only on manual testing is error-prone
Deleting tests hides problems instead of fixing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.