'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.



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

--package.json

"scripts": {
    "test": "jest"
  }

--jest.config.js

module.exports = {
    "testMatch": [
        "<rootDir>/tests/unit/*.test.js"
    ]
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piper
Solution 2 Sergiu Mare
Solution 3 Karl Adler
Solution 4 Imed Bargaoui