Node.js Quality Control & Assurance 5 — Questions and Answers
Question 1: What problem does `jest.useFakeTimers()` solve in unit tests?
- It replaces `setTimeout`, `setInterval`, and `Date` with controllable fakes so time-dependent code can be tested synchronously (Correct answer)
- It speeds up test execution by removing all real I/O delays
- It prevents tests from exceeding Jest's default 5-second timeout
- It mocks the system clock only for database timestamp columns
Correct answer: It replaces `setTimeout`, `setInterval`, and `Date` with controllable fakes so time-dependent code can be tested synchronously
Fake timers let you advance time with `jest.advanceTimersByTime()` or `jest.runAllTimers()` without waiting for real clock ticks.
Question 2: What does the `--bail` option do when running Jest?
- Stops the test run immediately after the first test suite failure (Correct answer)
- Skips all remaining tests after a specified number of failures
- Runs only tests that have previously failed
- Marks all tests as pending after one failure is detected
Correct answer: Stops the test run immediately after the first test suite failure
`--bail` (or `-b`) causes Jest to exit as soon as any test suite fails, useful in CI to get fast feedback without running the full suite.
Question 3: What is property-based testing, and which library implements it for Node.js?
- Generating random inputs to find edge cases automatically; `fast-check` is a popular library (Correct answer)
- Writing tests based on documented API contracts; `Pact` is used for this
- Testing all permutations of function arguments; `combinatorial-test` handles this
- Verifying configuration properties at startup; `convict` validates this
Correct answer: Generating random inputs to find edge cases automatically; `fast-check` is a popular library
`fast-check` generates hundreds of random inputs per test run, attempting to falsify a property (invariant) that should always hold.
Question 4: In Node.js, what is the role of `--require` when running Mocha?
- It loads a module before test files are executed, used for setup like registering Babel or loading env vars (Correct answer)
- It marks all tests in the specified file as required (non-skippable)
- It forces Mocha to use CommonJS require instead of ESM import
- It installs missing test dependencies at runtime
Correct answer: It loads a module before test files are executed, used for setup like registering Babel or loading env vars
`mocha --require @babel/register` is a common pattern that transpiles ES modules before tests run without changing test files.
Question 5: What is the primary goal of mutation testing tools like Stryker in a Node.js project?
- To evaluate how well the test suite detects bugs by introducing small code changes (mutations) and checking if tests fail (Correct answer)
- To find security vulnerabilities by fuzzing function inputs
- To measure code coverage by tracking which mutations are executed
- To automatically fix failing tests by mutating assertion values
Correct answer: To evaluate how well the test suite detects bugs by introducing small code changes (mutations) and checking if tests fail
Stryker generates mutants (e.g., changing `>` to `>=`) and reports a mutation score based on how many mutants the test suite catches.
Question 6: Which HTTP status code should a well-designed health check endpoint return when the service is fully operational?
- 200 OK (Correct answer)
- 204 No Content
- 202 Accepted
- 201 Created
Correct answer: 200 OK
A `200 OK` response signals to load balancers and monitoring tools that the service is healthy and ready to accept traffic.
Question 7: What does `process.exitCode = 1` do differently from `process.exit(1)` in a Node.js test script?
- It sets the exit code so the process exits with failure once the event loop drains naturally, without forcing immediate termination (Correct answer)
- It immediately kills the process with exit code 1, skipping all pending callbacks
- It sets the exit code only for child processes spawned after the assignment
- It schedules an exit after all pending promises resolve
Correct answer: It sets the exit code so the process exits with failure once the event loop drains naturally, without forcing immediate termination
Setting `process.exitCode` lets the current async operations and cleanup handlers finish, while `process.exit()` terminates immediately.
What problem does `jest.useFakeTimers()` solve in unit tests?