Run And Debug Test Cases Using Jest With Different CLI Options
Overview
In this article, We will be looking at different ways to run Jest test cases also we will be looking at debugging test case in Jest
Jest CLI provides a different options to run unit test cases.
We can run Jest test cases as we want. I have described below some of the ways of to run test cases.
- Run all tests
- Run tests that are contain in a file
- Run all tests that are related to changed file
- Run tests related to path
- Run tests that match spec name
- Run only single test
- Skip test cases
- Run tests in watch mode
Prerequisite
You need to have Node installed in order to use npm (node package manager).
After installing node we require Jest Package which will be downloaded from
NPM package store
Jest CLI Options
Run all tests
By Default Jest runs all tests
jest
Run tests that are contain in a file
Run tests that were specified in file
jest testfilename.js
jest path/to/testfilename.js
Run all tests that are related to changed file
Run tests related to changed files based on version tools (like git)
jest -o
Run tests related to path
Following command will runs test which are related
jest --findRelatedTests path/to/A.js path/to/B.js
Run tests that match spec name
Following command will run tests that matches spec name (against the name specified in describe
or test
)
jest -t spec-name
Run only single test/ file
Following command only run test that has keyword only
after describe
ortest
or it
The single test in the suite that we want to run should be changed to it.only() or test.only() as in the example below:
describe('Test suite', () => {
test('add: 2+2 equal to 4', () => {
expect(2+2).toEqual(4);
});
test.only('sub: 4-2 equal to 2', () => {
expect(4-2).toEqual(2);
});
});
Jest will run only the second test, and will show the others as skipped.
If we use test.only() multiple times in the file, then it will run only that test cases that has only
keyword.
Skip test cases
As there is way to run only some tests, Also there is way to skip some tests.
We need to use it.skip() or test.skip() for test cases that we want to skip.
Run tests in watch mode
Run tests watch mode means it will run after we have done some changes in code
Following is the command for same:
jest --watch #runs jest -o by default
jest --watchAll #runs all tests
How to Debug Jest Test Cases?
To Debug Jest test case, We need to follow some Steps as mentioned below:
Step 1: Add debugger;
statement in your test case which you want to debug.
Step 2: Execute this command in your project directory.
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]
This will run Jest in a Node process Note that the process will pause until the debugger has connected to it.
Step 3: We can connect to the Node.js Inspector using Google Chrome, open your browser and go to chrome://inspect
link and click on “Open Dedicated DevTools for Node”. It will open Dev Tools for Node.js
Step 4: Now debugger attached, The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script.
Click on the “play” button in the upper right hand side of the screen to continue execution. When Jest executes the test that contains the debugger;
statement, execution will pause and then you can examine the call stack.
This is the way you can run and debug Jest Test Cases.
Please like and share this article if you like it.
Thank you…
Comments
Post a Comment