Testing JavaScript Interview Questions and Answers
Understand unit, integration, and end-to-end testing and how tests improve confidence in JavaScript changes.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
A unit test checks a small piece of behavior in isolation, such as a function's output for given inputs. An integration test checks that multiple parts work together, such as a component calling a service. Both should assert observable behavior rather than implementation details.
A test should assert the behavior that matters to a caller: the returned value, a visible result, or a documented side effect. Use representative inputs, include important edge cases, and avoid assertions tied to private implementation details that may change without changing behavior.
A unit test checks a small unit of behavior in isolation, such as a function's output for a given input. Good unit tests are focused, repeatable, and explicit about expected results.
An assertion compares an actual result with an expected condition and fails the test when that condition is false. Assertions make the behavior under test explicit.
Isolated tests do not depend on hidden state or execution order, so they can run independently and failures are easier to diagnose. Reset or replace shared state and external dependencies where needed.
A stub supplies controlled behavior or data to a test. A mock additionally records or verifies interactions, such as whether a dependency was called with expected arguments.
Boundary cases exercise transitions and edge inputs where off-by-one, empty-value, and limit errors commonly occur. Identify boundaries from the function's contract, not just from implementation details.
A deterministic test produces the same result for the same setup, independent of timing, environment, or unrelated test order. Control nondeterministic inputs such as time, randomness, and external services.
The test pyramid encourages many fast, focused unit tests, fewer integration tests, and a small number of broad end-to-end tests. The balance aims for useful confidence without making the suite slow or fragile.
Assert that the call throws the expected error type or meaningful message, and ensure the assertion is scoped to the call under test. Avoid a test that passes for any unrelated exception.
Property-based testing generates many inputs and checks general invariants rather than only a few hand-picked examples. Good properties capture the contract, and failing cases can often be shrunk to a simpler counterexample.
Mutation testing introduces small code changes, or mutations, and checks whether the test suite detects them. Surviving mutations can reveal weak assertions, though not every mutation represents a meaningful defect.
A test may pass before asynchronous work completes if it neither returns nor awaits the relevant Promise. Return or await that Promise so rejections and assertions are observed by the test runner.
Pass side-effecting collaborators such as clocks, storage, or network clients into the code that uses them, rather than hard-coding access to globals. Tests can then supply deterministic substitutes while production uses real adapters.
JavaScript Practical and Coding Interview Questions
Problem: Implement `clamp(value, min, max)` and write self-contained assertions for a middle value and both bounds. Throw an error when an assertion fails; do not use a test framework.
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function assertEqual(actual, expected, label) {
if (actual !== expected) {
throw new Error(`${label}: expected ${expected}, got ${actual}`);
}
}
assertEqual(clamp(5, 0, 10), 5, "middle value");
assertEqual(clamp(-2, 0, 10), 0, "lower bound");
assertEqual(clamp(12, 0, 10), 10, "upper bound");
console.log("All checks passed");