Run Jest tests only for current folder

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.


To only run testing in a specific directory and to coerce Jest to read only certain type of files(my example: 'ExampleComponent.test.js' with new Jest version @24.9.0 you must write exact "testMatch" in jest.config.json || package.json in "jest" part next "testMatch": [ "<rootDir>/src/__tests__/**/*.test.js" ], This testMatch in my case hits all files with the prefix .test.js in tests/subdirectories/ and skips all other files like 'setupTest.js' and other .js files in 'mocks' subdirectory which is placed inside of 'tests' directory,so,my 'jest.config.json' looks like this

    {
        "setupFiles": [
            "raf/polyfill",
            "<rootDir>/setupTests.js"
        ],
        "snapshotSerializers": [
            "enzyme-to-json/serializer"
        ],
        "moduleNameMapper": {
            "^.+\\.(css|less|scss|sass)$": "identity-obj-proxy"
        },
        "testMatch": [
            "<rootDir>/src/__tests__/**/*.test.js"
        ]
    }

Just adapt to your needs 'testMatch' regex.

A little note: This is for [email protected] && [email protected] if it matters to anyone.

I hope it will be useful to someone, cheers all.


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