What is the main purpose of writing test cases in Node.js?
Test cases help check if your code works correctly by running small checks automatically. They catch errors early and make sure changes don’t break existing features.
Click to reveal answer
beginner
What does a typical test case include?
A test case usually has three parts: setup (prepare data or state), execution (run the code), and assertion (check if the result matches what you expect).
Click to reveal answer
beginner
In Node.js testing, what is an assertion?
An assertion is a statement that checks if a value or result is what you expect. If it’s not, the test fails and tells you there’s a problem.
Click to reveal answer
intermediate
Why use a testing framework like Mocha or Jest in Node.js?
Testing frameworks give you tools to write, organize, and run tests easily. They handle running tests, showing results clearly, and help write assertions.
Click to reveal answer
intermediate
What is a 'mock' in testing?
A mock is a fake version of a function or module used in tests to simulate behavior without running real code. It helps test parts of code in isolation.
Click to reveal answer
Which part of a test case checks if the output is correct?
AAssertion
BSetup
CExecution
DCleanup
✗ Incorrect
The assertion checks if the output matches the expected result.
What is the benefit of using a testing framework like Jest?
AIt helps write and run tests easily
BIt makes your code run faster
CIt automatically fixes bugs
DIt replaces the need for writing code
✗ Incorrect
Testing frameworks provide tools to write, organize, and run tests efficiently.
What does a 'mock' do in a test?
AGenerates random data
BDeletes test files automatically
CRuns the entire application
DSimulates a part of code to isolate testing
✗ Incorrect
Mocks simulate parts of code so you can test components without running real dependencies.
Which of these is NOT a typical part of a test case?
ASetup
BAssertion
CCompilation
DExecution
✗ Incorrect
Compilation is not part of writing test cases; it relates to building code.
Why write test cases before fixing bugs?
ATo make the code slower
BTo confirm the bug exists and prevent future errors
CTo avoid writing code
DTo delete old files
✗ Incorrect
Writing tests first helps confirm bugs and ensures fixes don’t break other parts.
Explain the three main parts of a test case and why each is important.
Think about preparing data, running code, and checking results.
You got /4 concepts.
Describe how using mocks can help when writing test cases in Node.js.
Imagine replacing a real part with a fake one to test only what you want.
You got /3 concepts.
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.