javascriptnode.jsjestjs

Jest code coverage not picking up JS files from parent directory even if within rootDir


I have a specific project setup where the Jest tests live in a folder but are testing code from two different source folders located in a parent directory. When trying to setup code coverage, I read on other posts that setting the rootDir to the common parent and adding the other folders as roots should solve it. However, my JS files in the source folders are still not being picked up. I've tried moving a JS file around to test and sure enough only files located within the same folder of the Jest config are being picked up for coverage.

Here's my jest config file:

{
  "testEnvironment": "jsdom",
  "testMatch": [
    "**/jest/**/*.test.js"
  ],
  "rootDir": "../../",
  "transform": {
    "^.+\\.[t|j]sx?$": [
      "babel-jest",
      {
        "configFile": "./babel.config.json"
      }
    ]
  },
  "transformIgnorePatterns": [
    "node_modules/(?!(parsel-js|cheerio|uuid)/)"
  ],
  "roots": [
    "<rootDir>/mu-plugins/tests",
    "<rootDir>/mu-plugins/core/assets/src/js",
    "<rootDir>/themes/theme/assets/ts"
  ],
  "setupFilesAfterEnv": [
    "<rootDir>/mu-plugins/tests/jest.setup.js"
  ],
  "moduleDirectories": [
    "<rootDir>/mu-plugins/tests/node_modules"
  ],
  "globals": {
    "CSS": {}
  },
  "collectCoverageFrom": [
    "**/*.js"
  ],
  "coveragePathIgnorePatterns": [
    "/node_modules/",
    "/coverage/"
  ],
  "coverageDirectory": "<rootDir>/mu-plugins/tests/coverage"
}

I am using Jest 29.7 in case it matters.

My rootDir used to be on the same level as the Jest config so I changed it to the common parent, and added all relevant directories to the roots array. This didn't solve the issue. I'm certain the paths are right though as when putting a wrong folder on purpose, Jest would throw an error.

I've tried clearing the Jest cache, but it didn't solve it either.

I've tried various formatting for collectCoverageFrom but none worked.

I've also tried changing the value of forceCoverageMatch, no luck there too.

When running jest --coverage I then see:

> jest --coverage

 PASS  jest/theme/components/shared/info.test.js
 PASS  jest/core/variant/variant.test.js
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------

Test Suites: 2 passed, 2 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        5.112 s
Ran all test suites.

Solution

  • I found what the issue was! By looking around more at similar issues online, I found this example repository. The main difference I had was that my package.json running the test was located in the same directory as my tests & jest config file, instead of at the root like in that example repository. More specifically, the issue is with jest-cli only.

    As I wanted to keep most dependencies contained in my tests folders, I added a small package.json file at the root directory, installed jest-cli only within it (and removed jest-cli from the tests directory as it wasn't needed anymore).

    Here's my root dir package.json:

    {
      "name": "root repo",
      "version": "1.0.0",
      "scripts": {
        "test": "jest --config=mu-plugins/tests/jest.config.json",
        "test:coverage": "jest --coverage --config=mu-plugins/tests/jest.config.json",
      },
      "devDependencies": {
        "jest-cli": "^29.7.0",
      }
    }
    

    I can now run npm run test:coverage to run the coverage from the root directory, but can still leave the tests specific dependencies (babel, enzyme, etc) in my tests folder.

    Most of my jest config file remained as is. Only change was to the babel's config file path as it seems to be relative to jest-cli running path, and is not able to use <rootDir>:

    {
      "^.+\\.[t|j]sx?$": [
        "babel-jest",
        {
          "configFile": "./mu-plugins/tests/babel.config.json"
        }
      ]
    }