reflex-testing-state

Generates pytest unit tests for Reflex.dev state classes, event handlers, and computed vars.

Updated Jun 13, 2026
One-click install
npx skills add https://github.com/jenreh/project-kit-template --skill reflex-testing-state-jenreh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: reflex-testing-state
Source: https://github.com/jenreh/project-kit-template/tree/main/.agents/agent-skills/skills/reflex-testing-state
Command: npx skills add https://github.com/jenreh/project-kit-template --skill reflex-testing-state-jenreh

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest, pytest-asyncio, pytest-cov, and includes references (resource) components.

What problem does it solve? Writing unit tests for Reflex.dev State classes requires knowing how to handle sync and async event handlers, streaming generators, computed vars, substates, and mocked external dependencies without a browser or running app. This Skill provides the patterns and fixtures to test Reflex state logic directly with pytest. ## Core Features & Use Cases - Handler Test Patterns: Covers sync handlers, async handlers with loading-flag assertions, streaming handlers consumed with async for, and background tasks with patched context managers. - State Logic Coverage: Tests base var defaults, computed vars via property assertions, and substates instantiated independently. - Mocked External I/O: Uses unittest.mock with AsyncMock to test API and database calls for both success and failure paths. - Use Case: When building a Reflex app with a ProjectState class that fetches data from an API, use this Skill to scaffold tests verifying the fetch handler populates items, resets is_loading on error, and handles timeouts. ## Quick Start Ask the AI to write pytest unit tests for your Reflex State class, covering its event handlers, computed vars, and mocked API calls.

Frequently Asked Questions about reflex-testing-state

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test Reflex State event handlers with pytest?

Reflex State is a plain Python class, so you can instantiate it directly and call handlers without a browser. Use pytest with pytest-asyncio: call sync handlers directly and await async handlers, then assert on state variables like is_loading.

How to test async event handlers in Reflex?

Enable asyncio_mode = "auto" in pytest configuration and write async test functions that await the handler. Assert that is_loading returns to False after completion, including error cases where the handler fails.

How do I mock API calls in Reflex state tests?

Use unittest.mock.patch with AsyncMock targeting the module path where the client is imported, such as myapp.state.api_client.post. Test both success responses and exceptions to verify error_message and loading state handling.

Can I test Reflex computed vars without running the app?

Yes, computed vars behave like properties on the state instance. Mutate the underlying base vars in your test, then assert the computed property returns the expected derived value.

When should I not use unit tests for Reflex state?

Unit tests on State classes do not cover browser behavior, UI component rendering, or end-to-end user flows. For those scenarios use Playwright integration tests instead of pytest state tests.