This script runs through all of the files in a given path and creates a /__tests__
folder containing jest snapshots if it thinks the file is a react component. It works with both React and React-native
To get started, use yarn or npm to install the package:
npm i jest-snapshot-generate -D
or yarn add jest-snapshot-generate -D
jest-snapshot-generate [your top - level react folder] [file extension, .js by default]
If a file named yourComponent-test.js
exists in your tests folder, the script will not do anything, just move along to the next file.
The output of the generated test files looks like this
import React from 'react';
import renderer from 'react-test-renderer';
import NameOfYourComponent from '../NameOfYourComponent';
test('Should render NameOfYourComponent correctly', () => {
const tree = renderer.create(<NameOfYourComponent />).toJSON();
expect(tree).toMatchSnapshot();
});
The script also targets sub-folders by running recursively through them all.
npm test
Feel free to improve :-)