Jest running tests by specific path
Asked Answered
E

3

5

currently I am using the standard testRegex logic to run my tests

  "jest": {
    "moduleFileExtensions": [
      "ts",
      "js"
    ],
    "transform": {
      "^.+\\.ts?$": "<rootDir>/node_modules/ts-jest/preprocessor.js",
      "^.+\\.js?$": "babel-jest"
    },
    "testRegex": "/tests/.*\\.(ts|js)$"
  }

But I would like to split them using specific paths

test-client   "testRegex": "/tests/client/.*\\.(ts|js)$"
test-server   "testRegex": "/tests/server/.*\\.(ts|js)$"

I see that there is a, option --runTestsByPath. but I cannot find any example of it

feedback welcome

UPDATE

I tried to add the script

"test-client": "jest   --runTestsByPath \"tests/client/\""

with a test file : tests/client/test.spec.js

but got an error :

$ jest   --runTestsByPath "tests/client/"
No tests found
No files found in /Users/../myapp.
Make sure Jest's configuration does not exclude this directory.
Expendable answered 2/1, 2019 at 10:54 Comment(3)
here are a few examples of usage: github.com/search?q=--runTestsByPath&type=CodeWarthman
Thanks @Paul , I tried to add it ( see my update in the question ... could not find such example ...Expendable
Possible duplicate of Run Jest test and collect coverage from all files in a specific directoryExpendable
C
5

I use the follow command and works:

jest "/client"

You can see the Jest documentation here: https://jestjs.io/docs/en/22.x/cli

Chinch answered 14/9, 2020 at 2:29 Comment(0)
B
2

--runTestsByPath needs the leading './'

I have got this working for myself:

jest --runTestsByPath "./directory/testFile.js"

and

jest --runTestsByPath "./directory"

Note I'm on WSL1, but should be the same for most flavours of linux.

Brewster answered 2/1, 2022 at 10:38 Comment(1)
This one worked for me , npx jest --runTestByPath ./testFile.js just using npx jest ./testFile.js was not working for me bcz it was saying using pattern .testFile.js to find test. I found this question after solving my problem :( T_TFiddler
E
-1
jest someDirectory --collectCoverageFrom=**/someDirectory/**/*.js
Expendable answered 2/1, 2019 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.