📖 Scenario: You are building a simple calculator module in Node.js. To make sure your calculator works correctly, you need to write test cases that check its functions.
🎯 Goal: Create a test file using Node.js and the built-in assert module to write test cases for a calculator module with add and subtract functions.
📋 What You'll Learn
Create a calculator object with add and subtract functions
Create a variable called testCases to hold test descriptions and expected results
Write test cases using assert.strictEqual to check the calculator functions
Add a final line to run all test cases and show success or failure
💡 Why This Matters
🌍 Real World
Writing test cases helps catch bugs early and ensures your code works as expected before sharing or deploying it.
💼 Career
Testing is a key skill for software developers to maintain code quality and reliability in professional projects.
Progress0 / 4 steps
1
Create the calculator object
Create a constant called calculator that is an object with two functions: add and subtract. Each function takes two parameters a and b and returns their sum or difference respectively.
Node.js
Hint
Use an object literal with arrow functions for add and subtract.
2
Create test cases array
Create a constant called testCases that is an array of objects. Each object should have description, func, args, and expected properties. Add two test cases: one for add(2, 3) expecting 5, and one for subtract(5, 2) expecting 3.
Node.js
Hint
Each test case is an object describing the test and expected result.
3
Write test runner with assertions
Import the built-in assert module with const assert = require('assert');. Then write a for loop that goes through each object in testCases. Inside the loop, use assert.strictEqual to check that calling func with args equals expected. Use the description as the message for the assertion.
Node.js
Hint
Use destructuring in the for loop and spread args when calling func.
4
Add final success message
After the for loop, add a line that logs 'All tests passed!' to the console.
Node.js
Hint
This message shows when all assertions succeed.
Practice
(1/5)
1. What is the main purpose of writing test cases in Node.js projects?
easy
A. To change the code style automatically
B. To make the code run faster
C. To add more comments to the code
D. To check if the code works as expected
Solution
Step 1: Understand the role of test cases
Test cases are written to verify that the code behaves correctly for different inputs and scenarios.
Step 2: Identify the main goal
The main goal is to check if the code works as expected, not to improve speed or style.
Final Answer:
To check if the code works as expected -> Option D
Quick Check:
Test cases = Verify code correctness [OK]
Hint: Test cases verify code correctness, not speed or style [OK]
Common Mistakes:
Thinking test cases improve code speed
Confusing test cases with code comments
Believing test cases change code style
2. Which of the following is the correct syntax to define a test case using Mocha in Node.js?
easy
A. describe('add function', () => { it('adds numbers', () => { /* test code */ }) })
B. function testAdd() { /* test code */ }
C. test('should add numbers', () => { /* test code */ })
D. runTest('add', () => { /* test code */ })
Solution
Step 1: Recall Mocha test structure
Mocha uses describe to group tests and it to define individual test cases.
Step 2: Match syntax with options
describe('add function', () => { it('adds numbers', () => { /* test code */ }) }) correctly uses describe and it with arrow functions.
Final Answer:
describe('add function', () => { it('adds numbers', () => { /* test code */ }) }) -> Option A
Quick Check:
Mocha uses describe and it [OK]
Hint: Use describe and it blocks for Mocha tests [OK]
Common Mistakes:
Using undefined functions like test() or runTest()
Not grouping tests inside describe()
Writing test code outside it() blocks
3. Given the following test case using Jest, what will be the output when running the test?
C. Using arrow function instead of regular function
D. No error, code is correct
Solution
Step 1: Check parentheses and braces
The it function is missing a closing parenthesis after the arrow function block.
Step 2: Verify assertion method and function type
The assertion to.equal is valid in Chai, and arrow functions are allowed in Mocha tests.
Final Answer:
Missing closing parenthesis for it() function -> Option B
Quick Check:
Check matching parentheses in test functions [OK]
Hint: Count parentheses carefully in test blocks [OK]
Common Mistakes:
Forgetting to close it() parentheses
Confusing assertion method names
Thinking arrow functions are disallowed
5. You want to write a test case that checks if a function filterPositive correctly returns only positive numbers from an array. Which test case correctly applies this in Jest?
hard
A. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBeTruthy(); });
B. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBe([2,3]); });
C. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toEqual([2,3]); });
D. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toContain(2,3); });
Solution
Step 1: Understand Jest matchers for arrays
toEqual compares array contents deeply, while toBe checks reference equality, which fails for arrays.
Step 2: Evaluate each option
it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toEqual([2,3]); }); uses toEqual correctly to check array contents. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBe([2,3]); }); uses toBe which fails for arrays. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toContain(2,3); }); uses toContain incorrectly with multiple values. it('filters positive numbers', () => { expect(filterPositive([-1,0,2,3])).toBeTruthy(); }); only checks truthiness, not exact output.