Edited and formatted by ericwm76
Eric's gist https://gist.github.com/ericwm76/e1204fc03f14af4429add8225ff55f71
Creating a React App
- In the terminal run
npx create-react-app NAMEOFYOURAPP - Cd into the new directory:
cd NAMEOFYOURAPP - Run
npm install. - You can run
npm startto see if the app was set up correctly.
Setting Up Testing
- Install enzyme:
npm i enzyme -D - Install enzyme-adapter-react-16:
npm install enzyme-adapter-react-16 -D - Inside of /src create setupTests.js:
touch src/setupTests.js - Put the following 3 lines of code in setupTests.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
- For snapshot testing, install enzyme-to-json
npm install enzyme-to-json -D - In package.json, add the following lines of code:
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
Don't forget the comma!
- Add an extra line in App.js (just so there's a change in the file) and save. Then run
npm testto check that the files are connected correctly. - Include the following lines as headers for all test files:
import React from 'react';
import { shallow } from 'enzyme';
import ClassName from './ClassName';