Node.js Quality Control & Assurance 3 — Questions and Answers
Question 1: What does `sinon.stub()` return when called without a replacement implementation?
- A stub that returns `undefined` by default (Correct answer)
- A stub that throws a TypeError on any call
- A spy that records calls but calls the original function
- A mock that auto-verifies expectations on restore
Correct answer: A stub that returns `undefined` by default
A Sinon stub without a configured behavior returns `undefined` and records all calls, arguments, and call counts.
Question 2: In the Node.js built-in `node:test` module (introduced in Node 18), how do you mark a test as a TODO?
- Pass `{ todo: true }` as the options object to `test()` (Correct answer)
- Prefix the test name with `TODO:`
- Call `test.skip()` with a `todo` flag
- Use `it.todo()` in the test body
Correct answer: Pass `{ todo: true }` as the options object to `test()`
The options object accepted by `test(name, options, fn)` supports a `todo` property that marks the test as planned but not yet implemented.
Question 3: What is a key advantage of snapshot testing in Jest?
- It detects unintended changes to component output by comparing against a stored snapshot (Correct answer)
- It measures rendering performance of React components
- It validates API response schemas against OpenAPI specs
- It automatically generates test data from live production traffic
Correct answer: It detects unintended changes to component output by comparing against a stored snapshot
When a snapshot test runs, Jest serializes the value and compares it to the stored `.snap` file, failing if anything changed unexpectedly.
Question 4: Which npm script convention is universally recognized by CI systems and package managers to run tests?
- "test": "..." in package.json scripts (Correct answer)
- "ci": "..." in package.json scripts
- "check": "..." in package.json scripts
- "validate": "..." in package.json scripts
Correct answer: "test": "..." in package.json scripts
`npm test` (or `npm run test`) executes the `test` script defined in `package.json`, and CI platforms like GitHub Actions run it by default.
Question 5: What does the `--exit` flag do in Mocha?
- Forces Mocha to quit after all tests finish, even if the event loop is still active (Correct answer)
- Exits with code 0 regardless of test results
- Skips teardown hooks before exiting
- Terminates the process after the first test failure
Correct answer: Forces Mocha to quit after all tests finish, even if the event loop is still active
Without `--exit`, Mocha waits for the event loop to drain; `--exit` calls `process.exit()` after tests complete, useful when open handles prevent exit.
Question 6: In continuous integration, what is a "flaky test"?
- A test that passes sometimes and fails other times without code changes (Correct answer)
- A test that always fails in CI but passes locally
- A test with very slow execution that exceeds timeouts
- A test that modifies shared state and breaks other tests
Correct answer: A test that passes sometimes and fails other times without code changes
Flaky tests have non-deterministic behavior caused by timing issues, random data, shared state, or network dependencies, eroding trust in the test suite.
Question 7: What is the purpose of `husky` in a Node.js project's QA workflow?
- To run scripts (like lint and tests) automatically at Git hook points such as pre-commit (Correct answer)
- To generate code coverage badges for the README
- To enforce semantic versioning on npm publishes
- To monitor test execution time and flag slow tests
Correct answer: To run scripts (like lint and tests) automatically at Git hook points such as pre-commit
`husky` makes it easy to configure Git hooks in `package.json`, ensuring linters and tests run before commits or pushes are accepted.
What does `sinon.stub()` return when called without a replacement implementation?