A testing utility for Preact components that provides virtual DOM access and an enzyme-like API.
preact-render-spy is a testing utility for Preact components that allows developers to render components and inspect the resulting virtual DOM. It solves the problem of testing Preact components in isolation or full depth by providing an API similar to Enzyme, with methods to find elements, simulate events, and assert on component state and output.
Preact developers who need to write unit and integration tests for their components, especially those familiar with Enzyme's testing patterns or looking for a Preact-specific testing solution.
Developers choose preact-render-spy because it's tailored for Preact, offers both shallow and deep rendering, integrates with Jest snapshots, and provides a straightforward API for testing component behavior and lifecycle methods without requiring a full DOM environment.
Render preact components with access to the produced virtual dom for testing.
Offers both shallow and deep rendering with configurable depth, enabling isolated component testing or full tree rendering for comprehensive integration tests.
Provides intuitive methods such as find(), simulate(), and setState(), making it easy for developers accustomed to Enzyme to write tests without a steep learning curve.
Includes a built-in snapshot serializer that seamlessly integrates with Jest for visual regression testing of Preact component outputs.
Directly supports testing lifecycle hooks like componentWillReceiveProps and componentWillUnmount through context.render() and other methods, as demonstrated in the examples.
The CSS-like selectors are restricted and do not support nesting, which can hinder complex DOM queries and make test code more verbose.
As admitted in the README, it lacks many options available in Enzyme, such as advanced traversal methods, which may limit its utility for teams migrating from Enzyme.
Requires manual setup for custom DOM implementations and has internal properties like SPY_PRIVATE_KEY that might inadvertently leak into component props, adding complexity.
Preact Render Spy is an open-source alternative to the following products:
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.