Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jest tests only for current folder

I have Jest installed on my machine and typing jest from terminal results in tests from parent folers also getting executed. I want to run tests only from the current folder.

For e.g. if I go to c:/dev/app in terminal and type some-jest-command, it should only run files with .test.js present in the app folder. Currently, running jest command from app folder runs tests in parent folders too, which is not my desired behaviour.

like image 843
darKnight Avatar asked Apr 02 '18 14:04

darKnight


People also ask

How do I run a Jest test of a specific folder?

If you want to run the tests from a specific folder user the --testPathPattern jest flag. When setting up the npm script add the path to the folder as well. In your package. json add the flag in you npm scripts.

Do Jest tests run concurrently?

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files. IMPORTANT: Paralellising tests mean using different threads to run test-cases simultaneously.

Where should Jest tests be stored?

src/file. test. js mentioned first in the Getting Started docs, and is great for keeping tests (especially unit) easy to find next to source files.

What is Jest watch mode?

% jest --watch. By default, Jest will run tests against all changed files since the last commit, but watch mode also exposes many options. By hitting the w key, you can see the different ways to operate Jest in watch mode. The options are dynamic, so it's worth playing around within watch mode to see what's available.


2 Answers

By default, Jest will try to recursively test everything from whatever folder package.json is located.

Let's say you're in c:/dev/app, and your package.json is in c:. If your basic command to invoke Jest is npm test, then try with run npm test dev/app.

like image 133
Andrew Miroshnichenko Avatar answered Sep 22 '22 14:09

Andrew Miroshnichenko


If you want to run the tests from a specific folder user the --testPathPattern jest flag. When setting up the npm script add the path to the folder as well. In your package.json add the flag in you npm scripts. Check the bellow code for an example.

"scripts": {     ....     "test:unit": "jest --watchAll --testPathPattern=src/js/tests/unit-tests",     "test:integration": "jest --watchAll --testPathPattern=src/js/tests/integration",     "test:helpers": "jest --watchAll jest --findRelatedTests src/js/tests/unit-tests/helpers/helpers.test.js"     .... }, 

After that open the command line, change your directory where your project is and run unit test.

npm run test:unit 

or integration tests.

npm run test:integration 

or if you want a test only for one specific file run

npm run test:helpers 
like image 43
Sergiu Mare Avatar answered Sep 21 '22 14:09

Sergiu Mare