40
votes

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

scripts:{
    "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

6

6 Answers

37
votes

Add the directory name as an argument

scripts:{
    "test": "jest --verbose ./my-directory"
}
31
votes

Add configuration to your package.json.

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

https://jestjs.io/docs/en/configuration.html#testmatch-arraystring

6
votes

This article

https://bambielli.com/til/2018-09-09-moving-jest-config-out-of-root/

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

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

Add the --rootdir option to your command:

jest --verbose --rootDir=laratest

Or in scripts:

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.

2
votes

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: [
    "./",
    "../more-files/"
],
0
votes

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