Snapshot Testing With React JS And Jest Using TDD
As Developer, We are facing different types of bugs and errors, during development time. Some Bugs make developer life hell. So using testing framework or testing tool and TDD(Test Driven Development) are helpful for developer to create less and non critical bugs during developement.
There is Lot of Testing framework and tools for testing javascript or node js application. I am using Jest because, it is a native testing tool for React JS application.
Today I am discussing about snapshot testing technique. Snapshot tests is a very useful tool. By using this, you want to make sure your UI does not change unexpectedly.
A typical snapshot test case renders a UI component, create snapshot or copy of render component with testing data, then compares it to a reference testing copy. The test will fail if the reference copy is not match with testing copy.
Installation and Configuration
> npm install -g create-react-app
Create new Testing Project by using this command to cmd
or terminal
> create-react-app react-snap-testing
Install prop types to check the property type for state
and props
and function
of react component.
> npm install --save prop-types
Install test renderer
npm package to provide the functionality for snapshot testing with jest
> npm install --save react-test-renderer
Test Project Structure
- Create folder named
components
which will contains all the react component and test files. - Add
DataSource.json
file, which contains thearray
ofPeople
infomration, we can get json datasource from https://jsonplaceholder.typicode.com/users . then save it tojson
file. - Then Create
People.jsx
react component, which represent thesingle people
or which represent the single row of the table view. - Create
PeopleList.jsx
, react component which represent the table view ofPeople
. - Then Create Test File named
People.spec.js
(we can use.test.js
suffix toPeople.test.js
file) which contains the all the test related to snapshot testing.
Design and Test People Component
and Test
In Figure 1.1, I have added table row
which is represent single people
information from people list
.
We can use TDD(Test Driven Development) methodology to test the People component
and write the test case in Poeple.spec.js
file.
To Support snapshot testing, developer must need to install ‘react-test-renderer’ npm package.
Create the Test Case In People.spec.js file for Testing People.jsx functionality
In Figure 1.2, I write test case, which will create spanshot with test case related to empty <People />
component.
In Line 2, I import react-test-renderer
to create spanshot automatically when runing test file by npm test
or yarn test
.
In Line 7, renderer.create(<React Component />).toJSON()
is chain function.
renderer.create(<React Component />
create theTestRenderer
instance, this function does not use real dom, but it creates component tree in memory and create test assertions, which provide properties and methods such astoJSON()
,toTree()
,update()
We are using
toJSON()
, which will be available after usingrenderer.create()
function.toJSON()
function return a object representing rendered tree. That tree only contains platform-specific nodes like <div> or <View> and their properties. But It does not contains any user-written component.In Line 8,
expect(tree).toMatchSnapshot()
function will automatically create the__snapshots__
folder and create the component wisesnap
file when first time thetest
is run. The snapshot testing file extension will be<test file name>.snap
.
For Jest Testing, we need to know about two functions, which are very important during writing test for application
1. describe () -> This function, which use for binding the testing scope. describe() takes two arguments.
first argument takes `scope name` .
second argument takes function which takes one and more test cases2. it() -> this function is used for writing the test case.
it() takes two agrument.
first argument takes `tast case name`
second argument takes testing functions
Output of the Testing Code
Go To terminal
or cmd
and run command
> npm test
In Figure 1.3, After Runing the command we will get that output to terminal and create the snapshot file in __snapshots__
directory.
(1) It shows that, All the test case is passed successfully.
(2) react-test-renderer
automatically create snapshot folder with snap shot testing file,The below figure contains snap
test output in src/components/__snapshots__/People.spec.js.snap
file.
Welldone, We Are Completing Our First Snapshot testing
Check the People Component
with Single Objects
Test PeopleList.js Component with Spanshot Testing
- Create Seperate test file for Table Component named
PeopleList.jsx
file. - Then create test file named
PeopleList.spec.js
- For Testing with
PeopleList.jsx
file, we are usingDataSource.json
file.
Create PeopleList Component For Snapshot Testing
Create The Test File for PeopleList.spec.js
file to test the PeopleList Component
In line 4, I have import the
json
file, which containsJSON
array of data. In line 9, I pass thelstPeoples
json array tolstPeoples
properties of a<PeopleList />
. Then Run the test again withnpm test
command fromterminal
orcmd
. The output of the file we can find in__snapshots__
path namedPoepleList.spec.js.snap
file.
The Process of Updating the Snapshot Testing
- To create situation about updating the test, first we test the
<PeopleList />
component with one element. Then add two element so we need to update the snapshot test.
Now we can run Figure 3.1’s codes into terminal, It generate the Snapshot With One Element in PeopleList.spec.js.snap
file at__snapshots__
folder.
Then , If we want to test 2 element within this test case, first we add the another object in peopleList
array after line 13.
2. Then Run test, get an error in command line and test failed. The output of after updating test case is provide below
From terminal we get option w
. Press w
,then we get the list of actions for further operations
If we want to update the snapshot , we will press u
to update the test. Or we can press q
to quite the testing.
After Pressing the q
, testing will end. To solve the Error for updating the testing case.we can use the command below in terminal
or cmd
. It will update the test case and update the snapshot of the <PeopleList />
element.
> npm test -u