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
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. 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 This article will walk you through the various effective methods that aid run specific tests using Jest. 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. Read More: 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: 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 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 The above code performs 3 different string operations 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 Create a new filestringconcat.test.jsunder the string-operations folder and write a test to validate the string concat operation as below Create a new folder calledstring-vowels-testand make a new file under the same pamphletvowels.test.js.Use the below test code 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. 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. 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. Example: Output: 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: Example: Output: 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 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. 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: Output: Note: 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. 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: Output: 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 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. 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. 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. 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: 2. Use & # 8211; watch for Active Development: Activate vigil mode for specific files to re-run tests on changes: 3. Utilize Test Name Patterns: Use -t or & # 8211; testNamePattern to run only specific test blocks within a file: 4. Use & # 8211; runInBand for Single Test Execution: You can avoid concurrence issues if you are debug a individual file via: 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: Read More: 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. On This Page # Ask-and-Contributeabout this theme with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to Run Jest Tests for a Specific File/Folder
Overview
Overview of Jest Testing Framework
Why Should You Run Jest Tests for a Specific File/Folder?
Step by Step Guide to Set up Jest and Run Specific File
npm install jest
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]}//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 '');})//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 '');})//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);})Different mode to fulfill Jest Tests
1. Run Jest Test for a Specific File
npx jest & lt; filename & gt;
npx jest vowels.test.js
2. Run Jest tryout for a Specific Folder
npx jest & lt; folder_name & gt;
npx jest string-operations
3. Running a Single Test Using & # 8216; only & # 8217; or & # 8216; f & # 8217;
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);})//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);})npx jest vowels.test.js
4. Skipping Tests Using & # 8216; skip & # 8217; or & # 8216; x & # 8217;
//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);})npx jest vowels.test.js
5. Running Tests with a Name Pattern
npx jest -- testNamePattern 'Numbers ' –verbose //Or npx jest -t 'Numbers ' –verbose
6. Running Jest Tests using BrowserStack Automate
Browserstack.ymlcontains custom-made form. Refer to customize the parameterbrowserstack-node-sdk jest src/ & lt; test_name & gt; .test.js
Best Practices to Run Jest Tests for a Specific File/Folder
jest path/to/YourTestFile.test.js jest path/to/your/folder/
jest path/to/file.test.js -- watch
jest path/to/file.test.js -t= '' should display login error ''
jest path/to/file.test.js -- runInBand
`` script '': {'' test: login '': `` jest src/components/Login/ ''}Conclusion
Useful Resources for Jest
Related Guides
Automate This With SUSA
Test Your App Autonomously