Testing & Debugging Flashcards

Mocha, Chai, Jest, Supertest Test Pyramid Mocks, Spies, and Stubs Debugging Tools (Chrome DevTools, Node Inspector) (47 cards)

1
Q

What is Mocha?

A

Mocha is a JavaScript test framework for Node.js that provides a flexible way to write asynchronous tests using BDD or TDD style.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Jest?

A

Jest is a JavaScript testing framework developed by Meta that offers zero-config testing, built-in assertions, mocking, and code coverage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Supertest?

A

Supertest is a Node.js library used for HTTP assertions to test APIs by making requests to an Express app or any HTTP server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Test Pyramid?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a mock?

A

A mock is a simulated object or function used to isolate the behavior of code being tested by replacing real dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a spy?

A

A spy is a wrapper around a function that tracks how it is called, including arguments, return values, and call counts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a stub?

A

A stub is a function that replaces another function and returns controlled outputs for testing purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Chrome DevTools?

A

Chrome DevTools are a set of web development tools built into the Chrome browser, useful for debugging, profiling, and inspecting code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Node.js Inspector?

A

Node Inspector is a debugging tool for Node.js applications that allows step-by-step code execution and inspection using DevTools.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the advantage of Jest over Mocha?

A

Jest includes built-in mocking, coverage reports, snapshot testing, and requires zero configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a disadvantage of using Mocha?

A

Mocha requires additional libraries like Chai or Sinon for assertions, spies, and mocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the advantage of the Test Pyramid model?

A

It ensures faster, more maintainable test suites by emphasizing unit tests and limiting slow, flaky end-to-end tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a best practice for mocking?

A

Mock only external dependencies or services, not the code you’re testing directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a use case for Supertest?

A

Testing REST APIs in Express applications by simulating HTTP requests and verifying responses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a typical example of using Jest for a test?

A

test(‘adds numbers’, () => { expect(add(2, 3)).toBe(5); });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the impact of good testing on system design?

A

It encourages modular, decoupled components and facilitates easier refactoring and maintenance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does mocking affect architectural decisions?

A

Heavy use of mocks can indicate tightly coupled systems and may hide integration issues if overused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the performance impact of end-to-end tests?

A

E2E tests are slower and more brittle due to full-stack execution, so they should be minimized in number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a common debugging workflow in Node.js?

A

Add breakpoints using debugger, start Node with --inspect, and connect with Chrome DevTools.

20
Q

What is an example of a stub in Sinon?

A

const stub = sinon.stub(obj, 'method').returns('value');

21
Q

What is a real-world tradeoff with Jest’s snapshot testing?

A

Snapshots can become outdated or misleading if developers accept changes without reviewing them carefully.

22
Q

What is a potential gotcha when mocking?

A

Improper mocking may hide real bugs or give a false sense of correctness in tests.

23
Q

What should you test with unit tests?

A

Pure functions or isolated logic with no external dependencies like databases or APIs.

24
Q

What should you test with integration tests?

A

Interactions between components or services such as database queries or API calls.

25
What is a common gotcha with async tests in Jest or Mocha?
Forgetting to use `async/await` or not returning a promise may cause the test to pass before execution completes.
26
What are lifecycle hooks in Jest?
beforeAll, afterAll, beforeEach, afterEach – used to run setup/teardown logic around test execution.
27
What is code coverage?
A metric that indicates how much of your codebase is executed by your test suite.
28
What’s a Jest feature for mocking modules?
`jest.mock('module')` allows mocking entire modules easily.
29
What are spies used for in testing?
To observe if a function was called, how many times, and with what arguments, without replacing it.
30
How can DevTools help in debugging?
They allow stepping through code, watching variables, setting breakpoints, and inspecting the call stack and scopes.
31
What is a disadvantage of snapshot testing?
Snapshots can become brittle and don't provide semantic understanding of changes.
32
How does the test pyramid improve fault tolerance?
By detecting bugs early through unit tests, reducing reliance on flaky E2E tests.
33
What is a best practice for debugging production Node.js apps?
Use logging tools (e.g., Winston), structured logs, and APM tools like New Relic or Datadog instead of breakpoints.
34
How do you run a single test file in Jest?
`jest filename.test.js`
35
How can Supertest be integrated with Jest?
By using Supertest in a Jest test block: `await request(app).get('/endpoint').expect(200);`
36
What is a common interview question about Jest?
What are the differences between Jest and Mocha? How do you mock a function in Jest?
37
What is a common interview question about test design?
Explain the test pyramid and how you structure tests in a large application.
38
What is the benefit of automated testing in CI/CD?
Automated tests ensure code quality, catch regressions, and allow safe deployments.
39
What is a common debugging tool for Node.js in the terminal?
`node inspect` or `node --inspect-brk` for interactive debugging.
40
Why is test isolation important?
Test isolation prevents side effects between tests and makes results reliable and reproducible.
41
What are flaky tests?
Tests that sometimes pass and sometimes fail due to timing, randomness, or external dependencies.
42
What’s the difference between unit and integration tests?
Unit tests test isolated pieces of code; integration tests check how components work together.
43
What is a good rule for when to mock?
Mock when you want to control or observe external dependencies, not internal logic.
44
What’s the role of `expect` in Jest?
`expect` is the assertion function used to test values in Jest.
45
How do you skip a test in Jest?
Use `test.skip()` or prefix the test name with `x` (e.g., `xtest()` or `describe.skip()`)
46
How do you debug Jest tests?
Use `--inspect-brk` with Node, or add `debugger` and run `node --inspect-brk node_modules/.bin/jest`.
47
What is the benefit of combining Mocha with Chai and Sinon?
Chai provides expressive assertions, while Sinon adds powerful mocking and spying capabilities to Mocha.