Testing & Debugging Flashcards
Mocha, Chai, Jest, Supertest Test Pyramid Mocks, Spies, and Stubs Debugging Tools (Chrome DevTools, Node Inspector) (47 cards)
What is Mocha?
Mocha is a JavaScript test framework for Node.js that provides a flexible way to write asynchronous tests using BDD or TDD style.
What is Jest?
Jest is a JavaScript testing framework developed by Meta that offers zero-config testing, built-in assertions, mocking, and code coverage.
What is Supertest?
Supertest is a Node.js library used for HTTP assertions to test APIs by making requests to an Express app or any HTTP server.
What is the Test Pyramid?
The Test Pyramid is a concept that advocates for a balanced test suite: more unit tests, fewer integration tests, and even fewer end-to-end (E2E) tests.
What is a mock?
A mock is a simulated object or function used to isolate the behavior of code being tested by replacing real dependencies.
What is a spy?
A spy is a wrapper around a function that tracks how it is called, including arguments, return values, and call counts.
What is a stub?
A stub is a function that replaces another function and returns controlled outputs for testing purposes.
What are Chrome DevTools?
Chrome DevTools are a set of web development tools built into the Chrome browser, useful for debugging, profiling, and inspecting code.
What is the Node.js Inspector?
Node Inspector is a debugging tool for Node.js applications that allows step-by-step code execution and inspection using DevTools.
What is the advantage of Jest over Mocha?
Jest includes built-in mocking, coverage reports, snapshot testing, and requires zero configuration.
What is a disadvantage of using Mocha?
Mocha requires additional libraries like Chai or Sinon for assertions, spies, and mocks.
What is the advantage of the Test Pyramid model?
It ensures faster, more maintainable test suites by emphasizing unit tests and limiting slow, flaky end-to-end tests.
What is a best practice for mocking?
Mock only external dependencies or services, not the code you’re testing directly.
What is a use case for Supertest?
Testing REST APIs in Express applications by simulating HTTP requests and verifying responses.
What is a typical example of using Jest for a test?
test(‘adds numbers’, () => { expect(add(2, 3)).toBe(5); });
What is the impact of good testing on system design?
It encourages modular, decoupled components and facilitates easier refactoring and maintenance.
How does mocking affect architectural decisions?
Heavy use of mocks can indicate tightly coupled systems and may hide integration issues if overused.
What is the performance impact of end-to-end tests?
E2E tests are slower and more brittle due to full-stack execution, so they should be minimized in number.
What is a common debugging workflow in Node.js?
Add breakpoints using debugger
, start Node with --inspect
, and connect with Chrome DevTools.
What is an example of a stub in Sinon?
const stub = sinon.stub(obj, 'method').returns('value');
What is a real-world tradeoff with Jest’s snapshot testing?
Snapshots can become outdated or misleading if developers accept changes without reviewing them carefully.
What is a potential gotcha when mocking?
Improper mocking may hide real bugs or give a false sense of correctness in tests.
What should you test with unit tests?
Pure functions or isolated logic with no external dependencies like databases or APIs.
What should you test with integration tests?
Interactions between components or services such as database queries or API calls.