Node.js Quality Control & Assurance 4 — Questions and Answers
Question 1: What does `jest.fn()` create?
- A mock function that records calls and can be configured to return values (Correct answer)
- A real function wrapped with call tracking only
- A spy that always calls through to the original implementation
- A promise-returning stub for async functions
Correct answer: A mock function that records calls and can be configured to return values
`jest.fn()` returns a mock function with a `.mock` property tracking calls, arguments, return values, and instances.
Question 2: Which tool is commonly used to enforce consistent code formatting in Node.js projects?
- Prettier (Correct answer)
- ESLint
- Husky
- Istanbul
Correct answer: Prettier
Prettier is an opinionated code formatter that rewrites code to a consistent style, whereas ESLint focuses on code quality rules.
Question 3: In Supertest, what does `request(app).get('/users').expect(200)` verify?
- That the HTTP response status code is 200 (Correct answer)
- That the response body contains a `users` array
- That the request reaches the `/users` route handler
- That the response time is under 200 milliseconds
Correct answer: That the HTTP response status code is 200
`.expect(200)` asserts the response's HTTP status code equals 200, failing the test if the server returns a different code.
Question 4: What is the main purpose of Istanbul (nyc) in a Node.js project?
- To instrument JavaScript code and measure how much of it is covered by tests (Correct answer)
- To mock external HTTP calls during test execution
- To enforce coding standards through static analysis
- To run tests in parallel across multiple CPU cores
Correct answer: To instrument JavaScript code and measure how much of it is covered by tests
Istanbul instruments source code at the AST level to track which lines, branches, functions, and statements are executed during tests.
Question 5: Which assertion style does Chai's `expect` interface use?
- Chainable getter-based natural language assertions (Correct answer)
- Callback-based assertions that call a done function
- Promise-based assertions using `.then()` chains
- Decorator-based assertions applied to class methods
Correct answer: Chainable getter-based natural language assertions
Chai's `expect` style chains getters and methods like `expect(val).to.be.a('string').and.equal('hello')` to form readable assertions.
Question 6: What does setting `"strict": true` in a TypeScript `tsconfig.json` enable for QA purposes?
- A set of strict type-checking flags that catch more potential bugs at compile time (Correct answer)
- Runtime type validation for all function arguments
- Automatic null-safety checks at JavaScript runtime
- Enforcement of 100% test coverage before compilation
Correct answer: A set of strict type-checking flags that catch more potential bugs at compile time
`strict` enables flags like `strictNullChecks`, `noImplicitAny`, and `strictFunctionTypes`, catching a wider class of type errors during development.
Question 7: In a CI/CD pipeline, what is the correct order of quality gates?
- Lint → Unit tests → Integration tests → Deploy (Correct answer)
- Deploy → Lint → Unit tests → Integration tests
- Unit tests → Deploy → Lint → Integration tests
- Integration tests → Unit tests → Lint → Deploy
Correct answer: Lint → Unit tests → Integration tests → Deploy
Running faster, cheaper checks (lint, unit tests) before slower integration tests and deployment minimizes wasted time when early gates fail.
What does `jest.fn()` create?