Brief introduction about Jest Testing.
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
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
The test script runs
"test": "jest --verbose"
"author": "Jan Bodnar",
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> firstname.lastname@example.org test C:\Users\Jano\Documents\js\jest-test
> jest PASS ./math-utils.test.js
PASS ./users.test.jsTest Suites: 7 passed, 7 total
Tests: 2 skipped, 35 passed, 37 total
Snapshots: 0 total
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
To run an individual test, we can use the
npx jest testname command.
"test": "jest --verbose ./test-directory"
We can configure Jest to run tests in a specified test directory.