JavaScript Testing 4 — Questions and Answers
Question 1: What is the purpose of `beforeEach` in a Jest or Mocha test suite?
- Runs once before the entire test file loads
- Runs before each individual test in a describe block (Correct answer)
- Defines shared variables across files
- Sets the test timeout globally
Correct answer: Runs before each individual test in a describe block
beforeEach runs a setup function before every individual test (it/test) in its enclosing describe block.
Question 2: Which tool is commonly used for end-to-end testing of JavaScript web applications by automating a real browser?
- Jest
- Jasmine
- Playwright (Correct answer)
- Sinon
Correct answer: Playwright
Playwright by Microsoft automates real browsers (Chromium, Firefox, WebKit) for end-to-end testing of web applications.
Question 3: What does `jest.useFakeTimers()` allow you to do?
- Run tests faster by skipping timeouts
- Control setTimeout/setInterval manually without waiting real time (Correct answer)
- Replace the Date object with a fixed date
- Disable all async operations in tests
Correct answer: Control setTimeout/setInterval manually without waiting real time
jest.useFakeTimers() replaces timer functions so you can advance time programmatically using jest.advanceTimersByTime().
Question 4: In testing, what is a 'test fixture'?
- A testing framework plugin
- A fixed set of preconditions or data used to set up tests (Correct answer)
- A UI component used only during testing
- A way to mark tests as flaky
Correct answer: A fixed set of preconditions or data used to set up tests
A test fixture is known data or state set up before tests run to ensure consistent, reproducible test conditions.
Question 5: How does React Testing Library differ from Enzyme in its testing philosophy?
- RTL focuses on implementation details; Enzyme on user behavior
- RTL focuses on user behavior and DOM queries; Enzyme on component internals (Correct answer)
- RTL is for unit tests; Enzyme is for integration tests
- RTL requires a real browser; Enzyme uses JSDOM
Correct answer: RTL focuses on user behavior and DOM queries; Enzyme on component internals
React Testing Library encourages testing from the user's perspective using DOM queries, while Enzyme allows testing internal component state and methods.
Question 6: What does the `--watch` flag do when running Jest?
- Monitors memory usage during tests
- Re-runs tests automatically when source files change (Correct answer)
- Watches for new test files added to the project
- Enables verbose logging
Correct answer: Re-runs tests automatically when source files change
Jest's --watch mode monitors file changes and re-runs only the tests related to changed files, speeding up the feedback loop.
Question 7: What is property-based testing?
- Testing CSS properties of DOM elements
- Testing individual object properties in isolation
- Generating many random inputs to verify invariants hold across all cases (Correct answer)
- Testing that class properties are correctly initialized
Correct answer: Generating many random inputs to verify invariants hold across all cases
Property-based testing generates random test data to verify that certain properties or invariants always hold, rather than testing specific examples.
What is the purpose of `beforeEach` in a Jest or Mocha test suite?