JavaScript Testing 3 — Questions and Answers
Question 1: What does TDD stand for and what is its core cycle?
- Test-Driven Development; Red-Green-Refactor (Correct answer)
- Type-Driven Design; Plan-Code-Test
- Test-Debug-Deploy; Write-Run-Fix
- Test-Driven Design; Mock-Assert-Verify
Correct answer: Test-Driven Development; Red-Green-Refactor
TDD stands for Test-Driven Development and follows the Red (failing test) → Green (make it pass) → Refactor cycle.
Question 2: When using `jest.spyOn(object, 'method')`, what happens to the original method by default?
- It is replaced with a no-op function
- It is called through (original behavior preserved) (Correct answer)
- It throws an error if called
- It returns undefined always
Correct answer: It is called through (original behavior preserved)
jest.spyOn wraps the original method so it can be observed, but the original implementation still executes by default.
Question 3: Which Cypress command waits for an element to be visible before interacting with it?
- cy.find()
- cy.get() (automatically) (Correct answer)
- cy.wait()
- cy.exists()
Correct answer: cy.get() (automatically)
cy.get() automatically retries and waits for elements to exist and be interactable before the command proceeds.
Question 4: What is snapshot testing primarily used for in Jest?
- Performance benchmarking
- Detecting unintended UI or output changes (Correct answer)
- Testing database snapshots
- Capturing network responses
Correct answer: Detecting unintended UI or output changes
Snapshot testing serializes a component's output and compares it against a stored snapshot to catch unexpected changes.
Question 5: What is the difference between a stub and a spy in testing?
- Stubs record calls; spies replace implementations
- Spies record calls; stubs replace implementations with preset responses (Correct answer)
- They are identical concepts
- Stubs are for async code; spies are for sync code
Correct answer: Spies record calls; stubs replace implementations with preset responses
A spy records how a function is called while preserving behavior; a stub replaces a function with a controlled return value.
Question 6: How do you test that a Jest mock was called with specific arguments?
- mockFn.assert(args)
- expect(mockFn).toHaveBeenCalledWith(args) (Correct answer)
- mockFn.verify(args)
- expect(mockFn.args).toEqual(args)
Correct answer: expect(mockFn).toHaveBeenCalledWith(args)
expect(mockFn).toHaveBeenCalledWith(args) asserts the mock was called at least once with the specified arguments.
Question 7: What does `expect.assertions(n)` do in a Jest test?
- Limits the test to n assertions
- Verifies that exactly n assertions were called during the test (Correct answer)
- Skips the test if fewer than n assertions exist
- Sets test timeout based on n
Correct answer: Verifies that exactly n assertions were called during the test
expect.assertions(n) ensures the test fails if the expected number of assertions is not reached, useful for async tests.
What does TDD stand for and what is its core cycle?