tezvyn:

What does 'test behavior, not implementation' mean?

AI-drafted, machine-checkedSource: interviewintermediate
WHAT IT TESTS

testing philosophy.

OUTLINE

query elements the way a user finds them (text, role, label) and assert observable behavior; for a Button, fire press and assert the visible effect, not internal state.

WHAT THIS TESTS: Whether you understand the library's guiding philosophy and can apply it concretely to avoid brittle tests.

A GOOD ANSWER COVERS: React Native Testing Library is built on the principle that tests should resemble how users interact with the app. You query elements by what is observable, accessible text, accessibility role, or label, rather than by component internals or test ids when avoidable, and you assert on what the user can perceive. Testing behavior, not implementation, means you verify outcomes, not mechanics. For a Button with an onPress handler, you do not assert that some internal flag flipped or that a specific method ran; instead you find the button by its visible label, fire a press using the library's event helpers, and assert the user-visible consequence, for example that a confirmation message now appears or that a passed-in callback was invoked. Because the test depends only on the public behavior, refactoring the component's internals leaves it green.

COMMON WRONG ANSWERS: Asserting on private state, refs, or instance methods, which couples tests to implementation and breaks on harmless refactors. Over-relying on test ids instead of accessible queries. Snapshotting everything and treating diffs as failures. Testing that a function exists rather than that pressing produces an effect.

LIKELY FOLLOW-UPS: The query priority order, when test ids are acceptable, how fireEvent.press works, and why this approach improves accessibility coverage.

ONE CONCRETE EXAMPLE: A SubmitButton calls onPress to show a Thank you message. The test renders it, finds the button by its Submit text, fires a press, and asserts Thank you is now on screen. Renaming internal handlers or refactoring to hooks does not change this test because it only checks observable behavior.

Read the original → testing-library.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.