jest: [Bug]: config options set at root level aren't copied to `projects`

Version

29.5.0

Steps to reproduce

  1. Clone minimal repo: https://gist.github.com/davidjb/eb8803286d8e6b1f0ca0d95e231fe3ca
  2. npm i
  3. Mock project dir: mkdir -p path/to/example
  4. npm test -- --showConfig
  5. Observe config output where child project configuration has Jest’s defaults set and not the root-level config for testMatch, testPathIgnorePatterns, watchPathIgnorePatterns and so on (output below).

Expected behavior

The documentation for projects (https://jestjs.io/docs/configuration#projects-arraystring--projectconfig) states that:

With the projects option enabled, Jest will copy the root-level configuration options to each individual child configuration during the test run, resolving its values in the child’s context.

So, the expectation is that an option set at the global level would be applied to all project entries.

Actual behavior

Project config use Jest’s configuration defaults instead of the root-level config set. Interestingly, the global options don’t actually show up under globalConfig, either:


{
  "configs": [
    {
      [...]
      "testMatch": [
        "**/__tests__/**/*.[jt]s?(x)",
        "**/?(*.)+(spec|test).[tj]s?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      [...]
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    [...]
    "projects": [
      {
        "displayName": "example",
        "rootDir": "path/to/example"
      }
    ],
    [...]
    "testTimeout": 12345,
    [...]
  },
  "version": "29.5.0"
}

Additional context

Note that this was tested in both package.json and jest.config.js - the results are the same. Also, whilst this example focuses on testMatch, testPathIgnorePatterns and watchPathIgnorePatterns, the issue appears to affect all options I’ve tried - such as setupFilesAfterEnv too.

That said, some options (such as testTimeout) will automatically apply to all projects, presumably because they’re set at a global level and can’t be applied to a project. In this specific case trying to set testTimeout for a project results in a Validation Warning: Unknown option "testTimeout" with value 678 was found. This is probably a typing mistake. Fixing it will remove this message.

Environment

System:
    OS: macOS 13.4
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.2.0 - /opt/homebrew/bin/node
    Yarn: 3.4.1 - /opt/homebrew/bin/yarn
    npm: 9.6.6 - /opt/homebrew/bin/npm
  npmPackages:
    jest: ^29.5.0 => 29.5.0

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 2
  • Comments: 20 (10 by maintainers)

Most upvoted comments

Check discussion and motivations in #12871 which introduced the lines in question. Nobody was listening, so I gave up. Just open a PR with a revert or improve the explanation.

That is complex. Global config are not copied, only options from project config. What is global and project configs? How to explain that? Uff… Perhaps suggesting to run jest --showConfig and inspect the resolved configs is enough?


developers shouldn’t rely on --showConfig to understand how rootDir works only if you’re using projects. For instance, I’ve been using Jest for more than 4 years and I’ve never heard of --showConfig

That’s a quote from that discussion. This is why I think some more education around --showConfig could be good idea.

Here we talked about collectCoverageFrom which simply ignores the <rootDir> token. That is another part documentation is missing.

The projects implementation is useless because its related documentation is awful. Many things simply don’t work as described, others are unintuitive. I had to go one by one, splitting options between the main config file and the “projects” config files until they more or less worked.