Testing
242 bites tagged Testing — interview questions with model answers, and 60-second explainers.
Jest Matchers: Asserting Values in Your Tests
Jest matchers are assertion functions that check if a value meets a condition. You use them with `expect()` to verify function outputs, object properties, or promise states.
React Testing Library Queries: Find Elements Like a User
React Testing Library queries find elements by simulating how users see your UI, not by implementation details. Prioritize user-facing attributes like `getByRole` or `getByLabelText`.
Jest: The 'Batteries-Included' JavaScript Test Runner
Jest is a "batteries-included" JavaScript testing framework, bundling a runner, assertions, and mocking tools. It's a default for React apps but works for any JS project. The footgun is thinking it's only for React; it's a general-purpose tool.
Test Like a User: The React Testing Library Philosophy
React Testing Library's philosophy is to test components the way a user interacts with them, not by their internal implementation. This ensures refactors don't break tests. The footgun is querying by implementation details instead of what a user actually sees.
Appium: Test Your React Native App Like a Real User
Appium tests your app like a real user, tapping and scrolling on actual devices to catch bugs your unit tests miss. It's for verifying full user flows across iOS and Android. The footgun: relying only on unit tests gives a false sense of security.
Google Play Testing Tracks: A Funnel for Safer Releases
Google Play testing tracks act as a release funnel, moving from internal QA to public beta to catch bugs early. Use them for initial checks (Internal), private betas (Closed), or public tests on the Play Store (Open).
Detox: Gray-Box E2E Testing for React Native
Detox runs gray-box E2E tests by automatically synchronizing with your React Native app's activity. This eliminates flaky `sleep()` calls. Write one JavaScript test suite for iOS and Android that runs reliably on CI and real devices.
Testing Custom Hooks with `renderHook`
`renderHook` isolates a hook for testing by running it inside a tiny, dedicated component. Use it to verify a hook's logic and side effects without rendering a full UI. Footgun: Forgetting to wrap state updates in `act()` can cause flaky, unpredictable tests.
The `act()` Utility: Syncing Tests with React's Updates
The `act()` utility ensures your tests wait for React to finish updating the DOM before you make assertions. Use it to wrap component renders and state updates. The footgun is forgetting to use `await` with an async function, which can lead to flaky tests.
React Native & Jest: Automatic Native Module Mocking
Jest tests for React Native run in Node, not on a device, so native code won't work. The `react-native` preset automatically mocks native modules, letting you test components without a real device environment.
React Native Testing Library: Test User Behavior, Not Code
React Native Testing Library tests components from a user's perspective. Instead of checking internal code, you find elements by visible text and simulate presses, ensuring tests survive refactors. The footgun is overusing implementation details like `testID`.
Unit Testing React Native Components with Jest
Jest tests your React Native components without a device, like checking individual Lego bricks. Use it to verify a button's logic or how a component renders with different props. The footgun is forgetting to mock native APIs, leading to flaky tests.
Testing WebSockets in FastAPI
Test a WebSocket conversation by scripting both sides. FastAPI's TestClient provides a `websocket_connect` context manager to send messages and assert responses sequentially, which is crucial for testing chats or live data feeds.
Mocking with Pytest's monkeypatch
Pytest's `monkeypatch` is a temporary stunt double for your code, safely swapping out functions or environment variables for a single test. Use it to isolate tests from network calls or filesystem access.
Testing Async FastAPI with pytest-asyncio
To test async code, your tests must also be async. `pytest-asyncio` lets you write `async def test_...` functions to `await` operations like database checks after an API call.
Run One Test with Many Inputs using pytest.parametrize
Run one test function with many inputs using `@pytest.mark.parametrize`, avoiding repetitive code. It's ideal for checking a function against various inputs, edge cases, and expected failures. The footgun: mutable parameters like lists are passed by reference.
pytest Fixtures: Reusable Test Setups
Pytest fixtures are reusable functions for test setup, like creating sample data. Your tests request them by name as arguments, and pytest automatically runs them and injects the results.
FastAPI's TestClient: Test Your API Without a Live Server
FastAPI's TestClient simulates API requests in-memory, letting you test endpoints without a live server. Use it with pytest to verify status codes and responses. The main footgun is forgetting to `pip install httpx`, as it's a required dependency.
Overriding FastAPI Dependencies for Testing
Overriding dependencies lets you swap real components for fakes during tests. This is vital for isolating tests from external services like auth providers or databases, letting you control inputs and avoid slow, flaky network calls.
Beta Programs: Testing with Real Users Before Launch
A beta program is your final dress rehearsal with a real audience. It's for validating user experience and catching deal-breakers before a public launch, not just finding bugs.
Sinon.JS: Isolate and Inspect Code for Unit Tests
Sinon.JS lets you replace real functions with test doubles to check *if* and *how* they were called. Use it to fake network requests or control timers. The biggest footgun is forgetting to restore fakes, which causes tests to leak state and fail unpredictably.
Nock: Intercept and Mock Node.js HTTP Requests
Nock acts like a fake switchboard for your Node.js app's outgoing HTTP calls, redirecting them to pre-defined responses. This lets you unit test code that relies on external services, making tests fast, deterministic, and offline-capable.
E2E Testing: The Final Check, Not The Whole Strategy
E2E testing is a dress rehearsal for your app, simulating a full user journey. Use it sparingly for critical flows like checkout, as it tests all services together. The footgun is over-reliance: they are slow, brittle, and hard to debug.
Code Coverage Reporting with nyc/Istanbul
Code coverage reporting asks, "Which lines of my code did my tests actually run?" Use a tool like `nyc` to wrap your test runner (e.g., Mocha) and generate a report. The footgun is chasing 100% coverage, which doesn't guarantee quality.
Get Testing bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.