This tutorial help to write test cases using Jest libs. This tutorial will teach you how to run a single test using Jest. Jest has been the tool of choice for writing tests in JavaScript for years now.
Whats Jest
The Jest is a well-known JavaScript testing framework Jest was created by Facebook. It is primarily made for testing Node.js projects, libraries, and JavaScript applications, including front-end applications.
Its comprehensive and feature-rich environment for creating and running tests is provided by Jest. Many developers and teams choose it because of its emphasis on speed, simplicity, and ease of use.
How To Run Specific Tests using Jest
To run a specific test using Jest, you can use the test.only
or it.only
functions to isolate the execution of that particular test. You can run only a single test be it a describe or it, it can be done by adding a .only after the describe or the it.
We’ll create a module emp.js to test the file. The source code is as follows:
const axios = require('axios'); const helper = require('./helper'); async function getEmployees() { let data = []; try { const response = await axios.get(`https://dummy.restapiexample.com/api/v1/employees`); data = response.data; } catch(err) { console.log(`Error getting emps: ${err.message}`, err.stack); } return data; } async function getEmplAw () { const data = await getEmployees(); return data; } module.exports = { getEmplAw, };
Using test.only or it.only Method
In your test file, locate the specific test you want to run. It should be within a describe block or an it block.
Replace test or it with test.only or it.only for the specific test you want to run. For example:
it.only('should return empty array if no data is found', () => { }
The our test file code will be as follows:
describe('My Test Suite', () => { test('This test will not run', () => { // ... }); test.only('This test will run', () => { // ... }); test('Another test', () => { // ... }); });
In the above example, only the test with test.only will run, while the other tests will be skipped.
Now, I’ll save the changes to your test file. Open your terminal or command prompt and navigate to the root directory of your project.
Run the Jest command to execute the tests:
npx jest
Jest will execute only the specific test marked with test.only
or it.only
, and skip all other tests.
When developing or troubleshooting, this method enables you to concentrate on executing a single test without running the complete test suite. Just be sure to take the only modifier off once you’re finished running that particular test so that all tests can be run again.
Using live employee test files
Let’s create a test file an define ourtest cases.
const emps = require('../src/emps'); describe('Emps', () => { describe('EmplAw', () => { it('should return employees', () => { const data = { name: 'javascript', works: [{ name: 'Adam', }, { name: 'Joe', }] }; const emp = emps.getEmplAw(data); expect(emp.length).toBe(2); expect(emp).toEqual(['Adam', 'Joe']); }); it('should return empty array if no employees is found', () => { const data = { name: 'test', works: [] }; const emp = emps.getEmplAw(data); expect(emp).toEqual([]); }); }); });
You can test the suite by run with npm test -- --verbose
, it will yield the following output:
Results:
npm test -- --verbose > jest-tohavebeencalledwith@1.0.0 test > jest "--verbose" PASS test/emps.spec.js Emps getEmplAw ✓ should return employees (3 ms)
Jest Run Specific Test Using –testNamePattern flag
Alternatively, you can use the –testNamePattern flag when running Jest from the command line to match and execute specific tests based on their names. For example:
jest --testNamePattern="should do something specific"
This will match and run all tests that have the name “should do something specific” in their description.
Both testNamePattern
and test.only
allow you to run a specific test instead of executing the entire test suite. Pick the approach that best fits your needs and workflow.