Jest: Jest config inherits previous one in projects array

Created on 31 Jul 2018  路  5Comments  路  Source: facebook/jest

馃悰 Bug Report

We have a monorepo. Jest config in root:

"projects": [
    "<rootDir>/projectA/jest.js",
    "<rootDir>/projectB/jest.js"
]

projectA/jest.js:

const common = require('ourToolsPackage');

module.exports = Object.assign(common, {
    "setupTestFrameworkScriptFile": "<rootDir>/setup-jest.ts",
    "transform": {
        ".ts": "<rootDir>/../node_modules/ts-jest/preprocessor.js"
    },
    "globals": {
        "ts-jest": {
            "tsConfigFile": "./tsconfig.json"
        }
    },
    "moduleFileExtensions": ["ts", "js"],
    "modulePathIgnorePatterns": ["dist"],
    "roots": ["src/"]
});

projectB/jest.js:

const common = require('ourToolsPackage');

module.exports = Object.assign(common, {
    rootDir: '.',
    preset: "./node_modules/jest-preset-angular/jest-preset.json",
    "setupTestFrameworkScriptFile": "<rootDir>/setup-jest.ts",
    "globals": {
        "ts-jest": {
            "tsConfigFile": "src/tsconfig.spec.json"
        },
        "__TRANSFORM_HTML__": true
    },
    "moduleFileExtensions": ["ts", "js", "html", "json"],
    "modulePathIgnorePatterns": ["dist"]
});

ourToolsPackage:

module.exports = {
    collectCoverage: true,
    coverageReporters: ["text-summary", "lcov"],
    coverageDirectory: "coverage",
    reporters: ["default", "jest-junit"],
    testMatch: [
        "**/__tests__/**/+(*.)+(spec|test).+(ts|js)"
    ]
};

I run jest --verbose --debug in root of monorepo and config is:

{
  "configs": [
    {
      ...
      "globals": {
        "ts-jest": {
          "tsConfigFile": "./tsconfig.json"
        }
      },
      ...
      "name": "f450223cded2e13301d633b3bf280357",
      ...
      "rootDir": ".../projectA",
      "roots": [
        ".../projectA/src"
      ],
      "runner": "jest-runner",
      "setupTestFrameworkScriptFile": ".../projectA/setup-jest.ts",
      ...
      "testMatch": [
        "**/__tests__/**/+(*.)+(spec|test).+(ts|js)"
      ],
      ...
      "transform": [
        [
          ".ts",
          ".../node_modules/ts-jest/preprocessor.js"
        ]
      ],
      ...
    },
    {
      ...
      "globals": {
        "ts-jest": {
          "tsConfigFile": "src/tsconfig.spec.json"
        },
        "__TRANSFORM_HTML__": true
      },
      ...
      "name": "126a5d5b6fdd2817d38a004c77305b9a",
      ...
      "rootDir": ".../projectB",
      "roots": [
        ".../projectB/src"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupTestFrameworkScriptFile": ".../projectB/setup-jest.ts",
      ...
      "testMatch": [
        "**/__tests__/**/+(*.)+(spec|test).+(ts|js)"
      ],
      ...
      "transform": [
        [
          ".ts",
          ".../node_modules/ts-jest/preprocessor.js"
        ],
        [
          "^.+\\.(ts|js|html)$",
          ".../projectB/node_modules/jest-preset-angular/preprocessor.js"
        ]
      ],
      ...
    }
  ],
  "globalConfig": {
    ...
    "projects": [
      ".../projectA/jest.js",
      ".../projectB/jest.js"
    ],
    "rootDir": "...",
    "runTestsByPath": false,
    ...
  },
  "version": "22.4.4"
}

As you can see, in projectB's transform field, we have by config itself only one "transform" (it is set by jest-preset-angular). But if I run it from monorepo root, another (from projectA) transform field is added. Same thing for roots.

To Reproduce

I think you need just specify projects field in config as array of paths to jest configs in monorepo. And have differences in transform and roots fields (I didn't check other fields)

Expected behavior

Each config must be isolated;
Order of entries in projects should not change behavior

Link to repl or repo (highly encouraged)

If it will be extremely necessary, I'll spend time on it.

Run npx envinfo --preset jest

npx: installed 1 in 2.286s

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
  Binaries:
    Node: 8.9.1 - ~/.nvm/versions/node/v8.9.1/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 5.5.1 - ~/.nvm/versions/node/v8.9.1/bin/npm
Bug Help Wanted

Most helpful comment

You can see what is considered global config and what's considered project config here: https://github.com/facebook/jest/blob/master/types/Config.js

This is very confusing, and somewhat inconsistent. Would love to fix it at some point. S great first step would be to explicitly error out if global config is provided as project config, but that would just treat the symptom, not the underlying issue

All 5 comments

This seems to apply to more configuration properties.
As an example I have different collectCoverageFrom options set for different projects (e.g. I want to exclude Storybook files from coverage in projectB only).

Also if I configure different coverageThreshold options in each project, when running tests for all projects it won't respect my individual coverageThreshold and uses global coverageThreshold for all files (not sure if this is another issue).

You can see what is considered global config and what's considered project config here: https://github.com/facebook/jest/blob/master/types/Config.js

This is very confusing, and somewhat inconsistent. Would love to fix it at some point. S great first step would be to explicitly error out if global config is provided as project config, but that would just treat the symptom, not the underlying issue

global and per project configs could cause some issues https://github.com/facebook/jest/issues/7359

Just run through some of the setup for multiple projects and find this issue.
Not sure if this is the reason but i spot that you are calling object.assign() with common being the target.
Hope it helps.

I'm not sure if this helps, but I had a similar problem and I used the merge npm package to perform a deep merge AND clone. You're not cloning when performing the Object.assign() so you overwrite the first object's references.

Was this page helpful?
0 / 5 - 0 ratings