darKnight
darKnight

Reputation: 6481

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.

Upvotes: 74

Views: 109132

Answers (8)

skplunkerin
skplunkerin

Reputation: 2383

If your current directory path was /path/to/myproject, then you can do the following to have jest only run the tests in myproject/:

npx jest --watch myproject

If you don't want to type the name of the directory, use the subshell command $(basename "$PWD"), like so:

npx jest --watch $(basename "$PWD")
  • $(basename "$PWD") gets the base name of the current working directory ($PWD).
    • the basename part strips the directory path and returns only the last part (the directory name itself, ie myproject from /path/to/myproject).

Upvotes: 0

mkdior
mkdior

Reputation: 125

You can always target a specific describe('... clause, if your test file of interest contains a unique describe, you can run jest with the -t option.

Say I have the following test file:

...

describe('folder running text XYZ', () => {
    it('test A', () => { ... });
    it('test B', () => { ... });
    it('test C', () => { ... });
    it('test D', () => { ... });
});

...

You can target the test set above using the following command: jest -t "folder running test XYZ".

-t docs:

-t, --testNamePattern        Run only tests with a name that matches the regex pattern.

Upvotes: 0

Andrew Miroshnichenko
Andrew Miroshnichenko

Reputation: 2074

By default, Jest will try to recursively test everything in 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, it'd recursively check for all tests in c:.For a specific folder like mentioned before, try running npm test dev/app.

Upvotes: 74

Sergiu Mare
Sergiu Mare

Reputation: 1724

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 --testPathPattern=src/js/tests/unit-tests",
    "test:integration": "jest  --testPathPattern=src/js/tests/integration"       
    ....
},

If you want to watch as well for changes, use the watch flag:

{
  ...
  "test:unit": "jest --watch --testPathPattern=src/js/tests/unit-tests",
  ...
}

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

npm run test:unit

or integration tests.

npm run test:integration

Upvotes: 80

Matt
Matt

Reputation: 35251

yarn:

yarn test nameoffolder

npm:

npm test nameoffolder

For example, if you have a folder named widget and you only want to run the tests in the widget folder you would run this command.

yarn:

yarn test widget

npm:

npm test widget

Upvotes: 0

Kevin Beal
Kevin Beal

Reputation: 10849

From the root of your project, you can run jest <substring of files> and it will only run the test files which have the substring you added.

$ jest /libs/components
> [PASS] /libs/components/button.tsx

Upvotes: 1

Imed Bargaoui
Imed Bargaoui

Reputation: 39

--package.json

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

--jest.config.js

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

Upvotes: 0

Goran_Ilic_Ilke
Goran_Ilic_Ilke

Reputation: 858

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.

Upvotes: 15

Related Questions