Unit Testing
What is Shallow Renderer in React testing?
Shallow rendering is useful for writing unit test cases in React. It lets you render a component one level deep and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered.
For example, if you have the following component:
Then you can assert as follows:
TestRenderer
package in React?
What is This package provides a renderer that can be used to render components to pure JavaScript objects, without depending on the DOM or a native mobile environment. This package makes it easy to grab a snapshot of the platform view hierarchy (similar to a DOM tree) rendered by a ReactDOM or React Native without using a browser or jsdom
.
What is the purpose of ReactTestUtils package?
ReactTestUtils are provided in the with-addons
package and allow you to perform actions against a simulated DOM for the purpose of unit testing.
What is Jest?
Jest is a JavaScript unit testing framework created by Facebook based on Jasmine and provides automated mock creation and a jsdom
environment. It's often used for testing components.
What are the advantages of Jest over Jasmine?
There are couple of advantages compared to Jasmine:
- Automatically finds tests to execute in your source code.
- Automatically mocks dependencies when running your tests.
- Allows you to test asynchronous code synchronously.
- Runs your tests with a fake DOM implementation (via
jsdom
) so that your tests can be run on the command line. - Runs tests in parallel processes so that they finish sooner.
Give a simple example of Jest test case
Let's write a test for a function that adds two numbers in sum.js
file:
Create a file named sum.test.js
which contains actual test:
And then add the following section to your package.json
:
Finally, run yarn test
or npm test
and Jest will print a result: