Brief introduction about Jest Testing.
What is Testing? .Testing means checking whether the code returns the expected output.IT sectors aim to provide stable, reliable, and secure services to their customers. Before releasing into production environments, the production-ready software requires testing. Software testing has matured over time, replacing the manual process with automated testing. Automated testing enables a fast feedback loop that allows developers to find and errors soon after they make them, permitting them the confidence to rewrite the code without breaking existing features. Jest is a Javascript Framework designed and built by Christoph Nakazawa, a Facebook engineer Similarly, jest is one of the most popular testing frameworks which works with projects using Babel, TypeScript, Node.js, React, Angular, Vue.js, and Svelte. There are numerous testing frameworks that include Mocha, Jasmine, Nightwatch, Protractor, Selenium-Web driver, Puppeteer Library, Cypress, Karma test runner, etc.
However, Jest is rated number #1 JavaScript automation testing framework for 2018 by the stateofjs survey. Let’s take a look at the advantages of Jest.
Advantages Of Jest
- Provides useful features to test a single test or skip tests
- Jest comes with an interactive mode that runs all the affected tests for the code changes that you’ve made in your last commit.
- Provides documentation with numerous examples, also offers a CLI tool to control your tests easily.
There are many types of testing and tests fall into three main categories:
- Unit testing
- Integration testing
- UI testing
Setting up Jest
First, we install Jest. The thing that makes Jest special is that it provides an integrated framework that does not require any experience in the configuration. The tool is ready to use and if you are using npm you can set it up instantly by running the following command.
$ node -v
v11.5.0
We use Node version 11.5.0.
$ npm init -y
We initiate a new Node application.
$ npm i --dev jest
We install the Jest module with nmp i --dev jest
.
$ npm i -g jsonserver
$ npm i axios
We are going to use jsonserver
and axios
too.
The package.json
The test script runs jest
.
package.json
{
"name": "jest-test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "jest --verbose"
},
"keywords": [],
"author": "Jan Bodnar",
"license": "ISC",
"devDependencies": {
"jest": "^24.0.0"
},
"dependencies": {
"axios": "^0.18.0"
}
}
How do I know what to test?
- Import the function to test
- Give input to the function
- Define what to expect as the output
- Check if the function produces the expected output
Jest running tests
Tests are run with npm test
command. The test files must have the test
term in their names.
$ npm test> jest-test@1.0.0 test C:\Users\Jano\Documents\js\jest-test
> jest PASS ./math-utils.test.js
PASS ./arith.test.js
PASS ./arith-params.test.js
PASS ./arith-skip.test.js
PASS ./string-utils.test.js
PASS ./arith-mock.test.js
PASS ./users.test.jsTest Suites: 7 passed, 7 total
Tests: 2 skipped, 35 passed, 37 total
Snapshots: 0 total
Time: 5.19s
Ran all test suites.
This is a sample output running tests with Jest. This is a terse output. For more information, we can use the --verbose
option.
To run an individual test, we can use the npx jest testname
command.
scripts:{
"test": "jest --verbose ./test-directory"
}
We can configure Jest to run tests in a specified test directory.