Jest ignore __mocks__ directory inside root

Viewed 1708

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

3 Answers

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.

Related