DEV Community

Cover image for Generate tests for your React components with jest-test-gen
Giulio Dellorbo
Giulio Dellorbo

Posted on

Generate tests for your React components with jest-test-gen

Hey 👋

I’ve released v1.0.0 of the test scaffolding generator tool jest-test-gen, introducing support for React components 🎉

You can now pass a path to a source file that exports one or more React components and it will automatically detect them and generate an initial jest test file containing snapshot tests for each exported component.

The generated code uses react-test-renderer to get a serializable tree of components that is then used by jest for snapshot testing.

generated component snapshot test

When propTypes are available, the component’s props will be passed to the component for testing, as a placeholder for the value of the props, a comment containing the type of the expected prop is inserted. This makes it easy to provide suitable prop values for testing purposes and makes sure that all component props are being tested.

jest-test-gen will recognize both class-based and functional components.

To give it a try, you can install the CLI tool via npm:

npm install -g jest-test-gen
Enter fullscreen mode Exit fullscreen mode

and run it to get a generated test file:

jest-test-gen ~/demoproject/src/components/myReactComponent.js

Please ⭐️ the repo jest-test-gen on Github if you find it useful.

Discussion (0)