itaied
itaied

Reputation: 7107

jest regex from npm script

I want to execute 2 different test environments,
one for integration tests and the second is for unit tests.

The folders structure is something like this:

__tests__/ut/p/a/t/h/file.test.ts
__tests__/it/p/a/t/h/file.test.ts

I have come up with these regex:

(__tests__/ut/.*\.test)\.(tsx?|ts?)$
(__tests__/it/.*\.test)\.(tsx?|ts?)$

My scripts looks like this:

"test:ut": "jest \"(__tests__/ut/.*\\.test)\\.(tsx?|ts?)$\"",
"test:it": "jest \"(__tests__/it/.*\\.test)\\.(tsx?|ts?)$\"",

But when I run yarn test:ut I get:

yarn run v1.3.2
$ jest "(__tests__/ut/.*\.test)\.(tsx?|ts?)$"
No tests found
In /home/dev/sample
  20 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 20 matches
Pattern: (__tests__/ut/.*\.test)\.(tsx?|ts?)$ - 0 matches
Done in 0.49s.

Though, if I use the same pattern in a json configuration file it works.
I guess it something with how I execute the jest.
How can I fix it?

EDIT

Executing this:

"test:ut": "jest -c jest.json \"(__tests__\\/ut\\/.*\\.test)\\.(tsx?|ts?)$\"",

result with the no tests found.
But if I put the regex inside the configuration file, it does work:

{
  "testRegex": "(__tests__\\/ut\\/.*\\.test)\\.(tsx?|ts?)$",
  "transform": {
    "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json",
    "jsx"
  ]
}

Executing it as npm script I just comment out the testRegex from the config file.

Upvotes: 3

Views: 4895

Answers (1)

cham
cham

Reputation: 10732

I came here with a similar problem and what finally worked for me is the Jest CLI option --testPathIgnorePatterns. So for your problem, where you only have two test locations, I would exclude the /ut/ folder when doing the it tests, and vice versa:

"test:ut": "jest --testPathIgnorePatterns \"/(it)/\"",
"test:it": "jest --testPathIgnorePatterns \"/(ut)/\"",

Also, if it helps, I did not use testRegex in my Jest config, but rather:

"testMatch": [
  "/__tests__/.*\\.(ts|tsx|js)",
  "**/?(*.)(spec|test).ts?(x)"
],

Upvotes: 2

Related Questions