First step is create a new sandbox with the create-react-app base. Then add these three dependencies to your sandbox.
enzyme & enzyme-adapter-react-16 & @testing-library/react
The enzyme-adapter-react-16 name will slightly change with newer releases of react.
Next, create a file in your src folder and name it setupTests.js . This files goves jest with the ability to understand React.
|
1 2 3 4 5 6 7 |
// setTest.js import Enzyme from "enzyme"; import Adapter from "enzyme-adapter-react-16"; // React 16 Enzyme adapter, at the time of this writing. Enzyme.configure({ adapter: new Adapter() }); |
Also, insert this in to your package.json file.
|
1 2 3 |
"jest": { "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js" } |
Now, for our test scenario we will create and use a functional component ( using hooks ) that has 1 input ( that will use useState ) and 1 prop to use and test.
