Node.js Quality Control & Assurance 2 — Questions and Answers
Question 1: Which Node.js built-in module provides the `assert.deepStrictEqual()` method for comparing objects in tests?
- assert (Correct answer)
- util
- test
- inspector
Correct answer: assert
The `assert` module provides `deepStrictEqual()` which recursively checks that objects have the same properties and values using strict equality.
Question 2: In Jest, what does the `--coverage` flag generate?
- A code coverage report showing tested and untested lines (Correct answer)
- A list of failed test suites only
- A performance benchmark of test execution time
- A report of linting errors found during testing
Correct answer: A code coverage report showing tested and untested lines
`--coverage` instruments the code and generates an HTML/text report showing line, branch, function, and statement coverage percentages.
Question 3: What is the purpose of `nock` in Node.js testing?
- To intercept and mock HTTP requests made by the application (Correct answer)
- To create mock file system operations
- To simulate database transactions
- To stub native Node.js modules
Correct answer: To intercept and mock HTTP requests made by the application
`nock` intercepts outgoing HTTP requests and returns predefined responses, allowing tests to run without hitting real external services.
Question 4: Which command runs only tests whose names match a pattern in Jest?
- jest --testNamePattern="pattern" (Correct answer)
- jest --filter="pattern"
- jest --grep="pattern"
- jest --only="pattern"
Correct answer: jest --testNamePattern="pattern"
`--testNamePattern` (or `-t`) filters tests by matching the pattern against each test's full name including describe blocks.
Question 5: In Mocha, what is the role of the `done` callback passed to an asynchronous test function?
- It signals Mocha that the async test has completed or failed (Correct answer)
- It marks a test as pending for later implementation
- It skips the remaining assertions in the test
- It triggers the next test in the suite immediately
Correct answer: It signals Mocha that the async test has completed or failed
Calling `done()` with no argument signals success; calling `done(err)` with an error fails the test and reports the error.
Question 6: What does the ESLint rule `no-unused-vars` enforce?
- Variables that are declared but never read or used must be removed (Correct answer)
- Variables must always be initialized at declaration
- Only `const` declarations are allowed
- Variable names must follow camelCase convention
Correct answer: Variables that are declared but never read or used must be removed
`no-unused-vars` flags variables, function parameters, and imports that are declared but never referenced, reducing dead code.
Question 7: Which testing strategy verifies that individual units of code work correctly in isolation?
- Unit testing (Correct answer)
- End-to-end testing
- Load testing
- Smoke testing
Correct answer: Unit testing
Unit testing focuses on the smallest testable parts of an application (functions, methods, classes) in isolation from external dependencies.
Which Node.js built-in module provides the `assert.deepStrictEqual()` method for comparing objects in tests?