How to insall Jest to test React components
This post is a short tutorial how to install and use Jest testing framework to test React component
React is a JavaScript library for building user interfaces and Jest is a testing framework to test the React components.
To install Jest, use:
npm install --save-dev jest
Update package.json
“Scripts”: {
....
“test” : “jest”
....
}
example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
}
Crate files for testin, like:
function sum(a, b) {
return a + b;
}
module.exports = sum;
File below contains simple test for sum function above:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Run: npm test
This is how to set some basic test setup for Jest and React.
Reference:
My site is free of ads and trackers. Was this post helpful to you? Why not
Disqus is great for comments/feedback but I had no idea it came with these gaudy ads.