Specify jest test files directory


Javascript Problem Overview

Am new to unit testing and i wanted only to test files located in a specific directory only

How do i specify that i want tests to run to files only on a specific directory and ignore others

so i have installed jest via npm

  "jest": "^23.6.0",

And specified my test command in package.json via

    "test": "jest --verbose"

The above runs all the files but i want it to run for files in a specific directory eg  laratest directory only

How do i proceed

Javascript Solutions

Solution 1 - Javascript

Add the directory name as an argument

    "test": "jest --verbose ./my-directory"

Solution 2 - Javascript

Add configuration to your package.json.

"jest": {
  "testMatch": ["**/laratest/**/*.test.js"]


Solution 3 - Javascript

Inside ./jest.config.js you can add an array of directories to search

// A list of paths to directories that Jest should use to search for files in
roots: [

Solution 4 - Javascript

This article


suggests doing something like this in your config.js file, and I quote the article:

// jest.conf from inside `config/` directory
  rootDir: '../',
  globalSetup: {...},

Solution 5 - Javascript

Add the --rootdir option to your command:

jest --verbose --rootDir=laratest

Or in scripts:

    "test": "jest --verbose --rootDir=laratest"

I tried specifying the directory (i.e. jest --verbose ./laratest) in my own project but wasn't seeing the expected result. HTH.

Solution 6 - Javascript

You must be specific in order to avoid running other directories with the same name. For example, this runs only the test in clients:

yarn jest "$PWD/clients"

but this runs tests in any folder named clients:

yarn jest clients
# also: yarn jest ./clients

Solution 7 - Javascript

Mac solution (not tested on windows)

scripts: {
  "test": "export NODE_ENV=development && jest"

According to jest docs, the NODE_ENV will be set to test if it is already not set. Thus if you export it... it will pass through to jest.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeoffView Question on Stackoverflow
Solution 1 - JavascriptFuzzyTreeView Answer on Stackoverflow
Solution 2 - JavascriptCodeDrakenView Answer on Stackoverflow
Solution 3 - Javascriptow3nView Answer on Stackoverflow
Solution 4 - JavascriptKimball RobinsonView Answer on Stackoverflow
Solution 5 - JavascriptErik EriksonView Answer on Stackoverflow
Solution 6 - JavascriptConnor ClarkView Answer on Stackoverflow
Solution 7 - JavascriptKing FridayView Answer on Stackoverflow