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 start
to 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 test
to 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 COMPONENTNAME from './COMPONENTNAME';