How to Run Jest Tests for a Specific File/Folder

On This Page Overview of Jest Testing FrameworkMay 17, 2026 · 10 min read · Testing Guide

How to Run Jest Tests for a Specific File/Folder

Running the intact tryout retinue can be time-consuming if you are act on a declamatory project. Jest enables developer to run examination for a specific file or folder. Thus, it facilitates quicker feedback and improved debugging.

Overview

What is Jest Framework?

Jest is a popular open-source NodeJS framework initially germinate by Facebook. It can be used for component examination and end-to-end testing, and can be incorporate into democratic test automation tools.

Different ways to execute Specific Tests using Jest

  • Run Jest Test for a Specific File
  • Run Jest screen for a Specific Folder
  • Running a Single Test Using ‘ only ’ or ‘ f ’
  • Skipping Tests Using ‘ skip ’ or ‘ x ’
  • Running Tests with a Name Pattern
  • Running Jest Tests using BrowserStack Automate
  • Using -t when running tests

This article will walk you through the various effective methods that aid run specific tests using Jest.

Overview of Jest Testing Framework

Jest is a democratic open-source NodeJS model initially developed by Facebook. It can be utilize for component testing and end-to-end testing, and it can be integrated into popular test automation puppet such as,, etc. Jest has many advantages, some of which are listed below.

  • It requires minimum configuration to run the tests
  • It can be desegregate with popular NodJS front-end frameworks such as VueJS, React, Angular, etc.
  • It also supports popular end-to-end testing model like WebdriverIO, Playwright, etc.
  • Jest is an open-source framework and free to use
  • It furnish many built-in capabilities, such as skipping, running specific, and go tests free-base on regular reflection patterns.
  • A turgid set of matches to validate the trial results
  • Supports Asynchronous Testing
  • Debugging capability, Mocking, and faster executing are a few key features of Jest.

Read More:

Why Should You Run Jest Tests for a Specific File/Folder?

Jest support run exam in specific file or folders. This assist in many scenarios, such as, testing specific features, getting faster feedback, etc.

Consider thisexample: If you are developing a characteristic associate to search functionality, you may not be bothered about the checkout cart, login, etc. In such scenario, you can only fulfil the tests specific to the search functionality. Jest provides an easy syntax for running specific test.

Read More:

Step by Step Guide to Set up Jest and Run Specific File

Here are the steps to set up Jest and run specific file:

Pre-requisite:

Step 1: Configure Folders

Using the Folder Explorer, Create an empty directory (Example:JestDemo)

Step 2: Open the newly created directory in VSCode

Using Visual Studio Code,Click on File & gt; Open Folderand Choose newly created directory (JestDemo)

Step 3: Install Jest

In VS Code, Click on Terminal andChoose New Terminal

From the New Terminal, Execute the below bidding

npm install jest

Step 4: Let & # 8217; s create a sample component to test

Create a new file called stringoperation.js and transcript and paste the below code

module.exports.getVowelsCount= function (string3) {let vowelsCount = 0; let Vowels = `` aAeEiIoOuU ''; for (let i = 0; i & lt; string3.length; i++) {if (Vowels.indexOf (string3 [i])! == -1) {vowelsCount += 1;}} render vowelsCount;} module.exports.stringConcat= function (string1, string2) {return string1+string2;} module.exports.extractNumbersFromString = function (draw) {return string.match (/\d+/) [0]}

The above code performs 3 different string operations

  • getVowelsCount:returns the figure of vowels in the yield twine
  • stringConcat:Concatenates two strings and returns the concatenated string
  • extractNumbersFromString:Returns the numbers in the given twine

Step 5: Write a Jest tryout to validate the above operations

To interpret different jest operations, create a Jest test in the folder structure below.

Create a folder namestring-operationsand write tests.

Create a new fileextractnumber.test.js  under the string-operationsfolder and indite a test to validate the extract routine functionality as below

//extractnumber.test.js const {extractNumbersFromString} = require (' .. /stringoperations.js ') test ('Verify Numbers in String ', () = & gt; {var numbersInString = extractNumbersFromString (`` BrowserStack is No.1 tool '') wait (numbersInString) .toEqual (`` 1 '');})

Create a new filestringconcat.test.jsunder the string-operations folder and write a test to validate the string concat operation as below

//stringconcat.test.js const {stringConcat} = require (' .. /stringoperations.js ') test ('Verify String Concatenation ', () = & gt; {var concatString = stringConcat (`` BrowserStack '', `` Tool to essay '') expect (concatString) .toEqual (`` BrowserStackTool to test '');})

Create a new folder calledstring-vowels-testand make a new file under the same pamphletvowels.test.js.Use the below test code

//vowels.test.js const {getVowelsCount} = require (' .. /stringoperations.js ') trial ('Verify Vowels in String ', () = & gt; {var vowels = getVowelsCount (`` Browserstack is the democratic testing tool ''); expect (vowels) .toEqual (12);})

After the creation of the above folders and tryout files, your project should seem like this:

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Different mode to fulfill Jest Tests

This section help you to understand how to run the above created tests. As mentioned originally, Jest provides different ways to run the tests. You can run jest tests on a specific file (s), specific folder (s), or with a regular reflexion, and you can even hop specific tests.

1. Run Jest Test for a Specific File

In the above project, we have create different tests. Let & # 8217; s execute only a test to get the number of vowel in a give twine. The vowel count trial resides in the vowels.test.js file. To execute a single jest test follow the below syntax.

npx jest & lt; filename & gt;

Example:

npx jest vowels.test.js

Output:

2. Run Jest tryout for a Specific Folder

To run Jest tests for a specific pamphlet, the syntax remains the same as running tests for a specific file; rather of the file, you must pass the folder name. In the above task, you have created a folder calledstring-operations,which has two file namelyextractnumber.test.jsandstringconcat.test.js. If you fulfill the string-operations folder, then both tests should be executed.

Syntax:

npx jest & lt; folder_name & gt;

Example:

npx jest string-operations

Output:

3. Running a Single Test Using & # 8216; only & # 8217; or & # 8216; f & # 8217;

Jest supports fulfil individual test within the same file. To execute a individual test in a Jest you can use the & # 8220;only& # 8221; postfix to your tests such asit.only (), test.only (), describe.only () etc.

Read More:.

In the above-created project, modify the filevowels.test.jsfile to the below code

const {getVowelsCount} = require (' .. /stringoperations.js ') test ('Verify Vowels in First String ', () = & gt; {var vowels = getVowelsCount (`` Browserstack is the popular testing puppet ''); await (vowel) .toEqual (12);}) test ('Verify Vowels in Second String ', () = & gt; {var vowel = getVowelsCount (`` Browserstack is a tool ''); expect (vowels) .toEqual (7);})

In the above code, two tryout were make – & # 8216;Verify Vowels in First String& # 8216; and & # 8216;Verify Vowels in Second String & # 8217;. If you desire to accomplish only one of these tryout, you can modify the code below.

//vowels.test.js const {getVowelsCount} = require (' .. /stringoperations.js ') tryout ('Verify Vowels First in String ', () = & gt; {var vowels = getVowelsCount (`` Browserstack is the popular examination creature ''); expect (vowels) .toEqual (12);}) test.only ('Verify Vowels Second in String ', () = & gt; {var vowels = getVowelsCount (`` Browserstack is a tool ''); expect (vowels) .toEqual (7);})

test.only ()is added for the 2d test. In this case, though you execute thevowels.test.jsonly the 2nd exam will be executed and the first test will be skipped.

Example:

npx jest vowels.test.js

Output:

Note:

  • You can use onlypostfix to exam (), it () or describe
  • The fprefix besides performs the same operations but it is uncommitted for only it() and describe ()mapping. Examplefit (), fdescribe () etc.

4. Skipping Tests Using & # 8216; skip & # 8217; or & # 8216; x & # 8217;

There are two ways to skip a test in Jest:skip postfixor use thexprefix. Note that thex prefix is only available forit() and describe ()functions. Forexample: you can skip the tryout usingxit(), xdescribe (), etc.

However, theomission postfixis uncommitted for all different functions for exampletest.skip (), it.skip (),etc. Let us understand with an exemplar

From the vowels.test.js,you can modify the examination to skip the second test, as shown below.

//vowels.test.js const {getVowelsCount} = require (' .. /stringoperations.js ') test ('Verify Vowels First in String ', () = & gt; {var vowel = getVowelsCount (`` Browserstack is the popular testing instrument ''); expect (vowel) .toEqual (12);}) test.skip ('Verify Vowels Second in String ', () = & gt; {var vowel = getVowelsCount (`` Browserstack is a creature ''); ask (vowel) .toEqual (7);})

As you can see in the above code, a second test is marked with.skip(). When you execute the file, it fulfil only the first test, and the second test will be skipped

Example:

npx jest vowels.test.js

Output:

5. Running Tests with a Name Pattern

Jest supports running with specific patterns or keywords. For example, suppose you are working in search functionality. In that case, the test name usually contains the keyword & # 8220;search& # 8221; In such scenarios, you can run all the test with a keyword lookup or choose a regular aspect pattern. The& # 8211; testNamePatternor -t flagcan be utilize to perform such operations.

In the above project, consider if you want to run a test comprise & # 8220;Number& # 8220;. You can specify the design as observe below

npx jest -- testNamePattern 'Numbers ' –verbose //Or npx jest -t 'Numbers ' –verbose

6. Running Jest Tests using BrowserStack Automate

BrowserStack offers a robust cloud execution platform that incorporate seamlessly with a wide range of testing frameworks, from legacy systems to the modish in automation. and testing, crucial aspects of modern package testing, oftentimes demand significant base investments, which can be challenging and costly to maintain. BrowserStack address these challenges by furnish a scalable, efficient, and budget-friendly solution.

Talk to an Expert

supports integration with the Jest framework, allowing you to easy execute your Jest tests. You can run your Jest tests in the by making a simple configuration update—installing thebrowserstack-node-sdkpackage and creating abrowserstack.yml file.

This apparatus ensures that your tests are executed in real-world conditions across different browsers and program, volunteer a comprehensive testing solution.

Interestingly, if you have a Jest fabric you can easily fulfill them on the BrowserStack Automate platform use the below steps.

  • Install BrowserStack-node-sdkusing the commandnpm i -D browserstack-node-sdk @ latest
  • Setup entree key and username with the commandnpx setup & # 8211; username & lt; uname –key& lt; key & gt;
  • Configure yourbrowserstack.yml file.
    Browserstack.ymlcontains custom-made form. Refer to customize the parameter
  • Once everything is ready, but action the examination using the below command.
browserstack-node-sdk jest src/ & lt; test_name & gt; .test.js

Streamline your Jest testing with. Run tests on real device and browser without the need for complex base. Quickly integrate Jest, and focus on delivering high-quality applications with ease and efficiency.

Best Practices to Run Jest Tests for a Specific File/Folder

Given below are the best practices to run jest tests for a specific file/folder:

1. Use Precise Paths:Execute tests by render a comparative or absolute way to the file or folder:

jest path/to/YourTestFile.test.js jest path/to/your/folder/

2. Use & # 8211; watch for Active Development: Activate vigil mode for specific files to re-run tests on changes:

jest path/to/file.test.js -- watch

3. Utilize Test Name Patterns: Use -t or & # 8211; testNamePattern to run only specific test blocks within a file:

jest path/to/file.test.js -t= '' should display login error ''

4. Use & # 8211; runInBand for Single Test Execution: You can avoid concurrence issues if you are debug a individual file via:

jest path/to/file.test.js -- runInBand

5. Avoid Global Setup for Targeted Runs:Avoid unnecessary global setup or mocks when running a individual file.

6. Create NPM Scripts for Common Test Targets: Set up customs playscript in package.json:

`` script '': {'' test: login '': `` jest src/components/Login/ ''}

Read More:

Conclusion

Running Jest tests for a specific file or folder is a great way to accelerate up development and debugging. By direct the relevant trial, you can facilitate faster feedback, isolate number more easily, and stay concentrate on the task at hand.

Streamline your Jest testing with BrowserStack Automate. Run tests on existent devices and browsers without the demand for complex infrastructure. Quickly integrate Jest and focus on delivering high-quality applications with comfort and efficiency.

Useful Resources for Jest

Tags
45,000+ Views

# Ask-and-Contributeabout this theme with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free