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
Add the directory name as an argument
scripts:{
"test": "jest --verbose ./my-directory"
}
Add configuration to your package.json.
"jest": {
"testMatch": ["**/laratest/**/*.test.js"]
}
https://jestjs.io/docs/configuration#testmatch-arraystring
"test:unit": "jest --testMatch '**/src/test/**/?(*.)+(spec|test).[tj]s?(x)'"
(I wasn't aware of that).
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/"
],
jest.config.js
If want to you post this as an answer to a question specifically on config for jest.config.js
I'll post a question and send it to you.
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: {...},
}
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.
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
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
.
Success story sharing
my-directory
.