- Posted by Neeraj Maurya
- On January 20, 2020
It is simple to set up – it is simple to install on its own. We can just install it directly using either npm or yarn.
How to get started for installation –
- First download node
- Then open node.js command prompt
- To find the version of node we are using Node -v
- Then install npm using command npm install
- Let’s Create a folder in the Desktop named “Jest”
- Then in command prompt use –
Finally, run yarn test or npm run test:
npm run test
Let’s get started by writing a test for a hypothetical function that subtracts two numbers. First, create a subtract.js file:
function subtract(a, b)
return a – b;
module.exports = subtract;
Then, create a file named subtract.test.js. This will contain our actual test:
const subtract = require(‘./subtract);
test(subtracts 4 – 2 to equal 3’, () =>
Add the following section to your package.json:
Finally, run yarn or npm run test and Jest will print this message:
✓ subtracts 4 – 2 to equal 2 (5ms)
Advantages of using Jest
It is compatible: – The framework is compatible with Angular, React, NodeJS, VueJS and other babel based projects.
Faster than other traditional tools: – It is a very fast testing tool. When our test is CPU bound, it can save significant time from our test runs. The local test used to take 45 minutes Jest dropped it to 14 to 15 minutes.
IT has it all: – It was based on Jasmine, so it has inherited all the good qualities from it. Jest has now been departed from Jasmine, yet they have kept the same functionality but have improved it.
Snapshot testing: – In testing, snapshot testing is a great tool that ensures that your application’s UI does not change unexpectedly between releases.
Timer Mocks:- Users can manipulate the Time which is useful. Therefore, the test will not be slow and time can be manipulated by it
Write Jest tests to:
- Test a component in isolation
- Test a component’s public API (@api properties and methods, events)
- Test basic user interaction (Like -clicks)
- Verify the DOM output of a component
- Verify that events fire when expected