Skip to content

Automatically generate boilerplate for React component snapshot tests

License

Notifications You must be signed in to change notification settings

fagerbua/react-snapshot-test-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NOTE: This project is not actively developed. I (@fagerbua) don't use it for my own projects anymore. It only encouraged me to make too many snapshot tests. Rather than use this tool, I'd recommend you give some careful thought to which snapshot tests would create the most value for your application, and code them by hand. Good luck!

react-snapshot-test-generator

Travis build status

Automatically generate boilerplate for React component snapshot tests

Rationale

React Tree Snapshot Testing is great, but defining the tests generates a fair bit of boilerplate. If your code structure follows some simple conventions, react-snapshot-test-generator can do the work for you.

Getting started

Requirements

If you're new to snapshot testing with React and Jest, check out the official tutorials (React, React Native) and get your first test working before continuing.

The generator expects the component <MyComponent /> to be available as the default export of a file named MyComponent.js. If your code has a different structure, you may want to modify the generator script accordingly.

Installation

Once you have the test renderer working with Jest, run

npm install --save-dev react-snapshot-test-generator

in your project folder.

Configuration

Before generating tests, you need to specify your components. Create a file named snapshotTestConfig.js in your project root folder (see Custom options if this doesn't suit you) that looks like this:

module.exports = {
  componentDefinitions: {
    'components': [
      'MySimpleConstantComponent', // Causes './components/MySimpleConstantComponent' to be imported
                                   // and generates a snapshot test for <MySimpleConstantComponent />
    ],
    'widgets': [
      'MySimpleWidget', // Causes './widgets/MySimpleWidget' to be imported and generates a snapshot
                        // test for <MySimpleWidget />
      ['MyComplexWidget', [ // Combinations of props are specified as plain objects. The enclosing array is mandatory.
        {aBooleanProp: "false", aStringProp: "'value'"}, // note double quoting for string values
        {aBooleanProp: "true", aStringProp: "'value'"},
      ]], // Causes './widgets/MyComplexWidget' to be imported and generates snapshot tests for
          // <MyComplexWidget aBooleanProp={false} aStringProp={'value'} /> as well as
          // <MyComplexWidget aBooleanProp={true} aStringProp={'value'} />
    ],
  },
  autoMocks: ['TextInput'], // Optional: automatically generate simple mocks. Particularly useful
                            // for React Native.
}

In package.json, add scripts like the following for easy test generation:

scripts: {
  "generateTests": "react-snapshot-test-generator",
  "test": "npm run generateTests && jest"
}

Generating tests

Simply running npm run generateTests will read the configuration from snapshotTestConfig.js and generate a test file in __tests__/snapshotTests.js.

Custom options

The test generation can be customized. Run npm run generateTests -- -h to get information about the available command line options. Note in particular the -n flag for generating React Native tests.

Credits

Development of this tool is supported by the University of Oslo.

License

MIT

About

Automatically generate boilerplate for React component snapshot tests

Resources

License

Stars

Watchers

Forks

Packages

No packages published