Parameterized Testing

All Jest topics
∙ Jest

Parameterized Testing focuses on the same behavior across a table of inputs and expected outputs. It uses `test.each()` to confirm every table row satisfying the shared assertion.

📝Syntax
test.each(cases)("name", (input, expected) => { ... })
parameterized-testing.test.js
📝 Jest Example
👁 Expected Result
💡 Run the test from isolated state and read the matcher diff when it fails.
👀Output
Parameterized Testing: pASS — all table rows
🔍Line-by-Line Explanation
LineMeaning
test.each([[1, 2, 3], [2, 3, 5]])('adds %i + %i', (a, b, expected) => {In Parameterized Testing, line 2 implements setup, action, or verification for this example.
expect(a + b).toBe(expected);In Parameterized Testing, line 3 creates an expectation for the received value.
});In Parameterized Testing, line 4 implements setup, action, or verification for this example.
🌐Real-World Uses
  • 1Use Parameterized Testing to verify the same behavior across a table of inputs and expected outputs.
  • 2Parameterized Testing is valuable in real application testing when the test must prove every table row satisfying the shared assertion.
  • 3A useful failure record for Parameterized Testing contains the failing row values in the test name.
Common Mistakes
  • 1Parameterized Testing commonly fails because of putting unrelated scenarios into one unreadable table.
  • 2Starting Parameterized Testing without small named cases covering boundaries makes the result nondeterministic.
  • 3For Parameterized Testing, executing code without asserting every table row satisfying the shared assertion is incomplete.
  • 4Using Parameterized Testing to cover complex setup that differs per case creates the wrong test boundary.
Best Practices
  • 1Prepare small named cases covering boundaries before running Parameterized Testing.
  • 2Implement Parameterized Testing with `test.each()`.
  • 3Make the central Parameterized Testing assertion prove every table row satisfying the shared assertion.
  • 4Preserve the failing row values in the test name whenever Parameterized Testing fails.
💡Core behavior
  • 1Parameterized Testing target: the same behavior across a table of inputs and expected outputs.
  • 2Parameterized Testing API: `test.each()`.
  • 3Parameterized Testing expected result: every table row satisfying the shared assertion.
  • 4Parameterized Testing primary risk: putting unrelated scenarios into one unreadable table.
💡Implementation steps
  • 1Set up Parameterized Testing with small named cases covering boundaries.
  • 2For Parameterized Testing, invoke the behavior that produces the same behavior across a table of inputs and expected outputs.
  • 3In Parameterized Testing, apply `test.each()` to the observed result.
  • 4Finish Parameterized Testing by asserting every table row satisfying the shared assertion.
💡Verification
  • 1Run Parameterized Testing once with input that should satisfy every table row satisfying the shared assertion.
  • 2Add a negative Parameterized Testing case that must produce a readable failure.
  • 3Repeat Parameterized Testing from fresh state to reveal shared-data or ordering dependencies.
  • 4Diagnose Parameterized Testing through the failing row values in the test name.
💡Scope
  • 1Parameterized Testing covers the same behavior across a table of inputs and expected outputs.
  • 2Parameterized Testing does not directly prove complex setup that differs per case.
  • 3Mocks and fixtures used by Parameterized Testing must continue to match its real dependency contracts.
  • 4For evidence outside the Parameterized Testing process boundary, prefer separate focused tests.
Summary
  • Parameterized Testing setup: small named cases covering boundaries.
  • Parameterized Testing action: `test.each()`.
  • Parameterized Testing assertion: every table row satisfying the shared assertion.
  • Parameterized Testing diagnostics: the failing row values in the test name.
  • Parameterized Testing boundary: choose separate focused tests for complex setup that differs per case.
🧑‍💻Interview Questions
Q1. What does Parameterized Testing verify?
Answer: Parameterized Testing verifies the same behavior across a table of inputs and expected outputs.
Q2. Which Jest API is central to Parameterized Testing?
Answer: The central Parameterized Testing API is `test.each()`.
Q3. What proves Parameterized Testing passed?
Answer: A passing Parameterized Testing test shows every table row satisfying the shared assertion.
Q4. What makes Parameterized Testing unreliable?
Answer: A common Parameterized Testing cause is putting unrelated scenarios into one unreadable table.
Q5. When should another test type replace Parameterized Testing?
Answer: Replace Parameterized Testing with separate focused tests for complex setup that differs per case.
🎯Quick Quiz

Which approach correctly implements Parameterized Testing?