worg
worg

Reputation: 55

Jest ignore __mocks__ directory inside root

I have roughly the following structure

Root
├ __tests__
├ Directory0
│  ├ childA
│  │   └ __mocks__
│  │         └ module_a.js
│  └ childB        
│      └ __mocks__
│            └ module_a.js
├ Directory1
│    └ …
├ Directory2
│    └ …
└ DirectoryN

I'm trying to setup jest for the root directory, but completely ignoring Directory0 root tests would live in __tests__, some child directories have their own jest configuration and are tested separately, there are duplicated mocks [as those are templates that get copied, so no way to have a single shared mock] and jest is picking those mocks and emitting a warning.

I've already tried testPathIgnorePatterns: ['<rootDir>/Directory0/'] with no success

Upvotes: 4

Views: 3507

Answers (3)

Rbulmer55
Rbulmer55

Reputation: 41

Exactly what @Harrison Kamau said with "roots" config.

https://jestjs.io/docs/en/configuration#roots-arraystring

"jest": {
    "setupFiles": [
      "<rootDir>/jest/setEnvVariables.js"
    ],
    "testPathIgnorePatterns": [
      "/packages/"
    ],
    "roots": ["<rootDir>/src/"]
  },

Worked for me, ignored mocks in my packages folder.

Upvotes: 1

Harrison Kamau
Harrison Kamau

Reputation: 322

I'm sure this what you're looking for: Add __tests__ to your list of roots in jest.config.js:

module.exports = {
  roots: [`<rootDir>/__tests__`],
};

References:

https://github.com/facebook/jest/issues/2726#issuecomment-390625860

Upvotes: 1

Diogo Peres
Diogo Peres

Reputation: 1372

Add this to your package.json were you have the jest config

"collectCoverageFrom": [
  "**/*.{js,jsx}",
  "!**/Directory0/**"
],

Change the "**/*.{js,jsx}", to match your case.

Read more in here: https://jestjs.io/docs/en/configuration#collectcoveragefrom-array

Upvotes: -1

Related Questions