jest.config.js within monorepo setup doesn't load when run from rootDir

Created on 29 Jan 2018  ยท  13Comments  ยท  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
It seems a bug or a missing mention in the documentation.

What is the current behavior?
I've the below monorepo setup and when I run jest within each of the packages folder, jest picks up the jest.config.js within each of them and works perfectly. But when I try to run them from the project's rootDir, the tests fail because the setupFiles aren't being used(I think the jest.config.js is infact not being respected). Is there a way I could get this fixed?

.
โ”œโ”€โ”€ package.json           
โ”œโ”€โ”€ lerna.json
โ”œโ”€โ”€ jestBaseConfig.js  # Base jest configuration file which will be used in jest.config.js of each package
โ”œโ”€โ”€ packages 
โ”‚   โ”œโ”€โ”€ api
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jest.config.js
โ”‚   โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”‚   โ”œโ”€โ”€ src
โ”‚   โ”‚   โ””โ”€โ”€ test
โ”‚   โ”œโ”€โ”€ components      
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jest.config.js
โ”‚   โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”‚   โ”œโ”€โ”€ src
โ”‚   โ”‚   โ””โ”€โ”€ test
โ”‚   โ””โ”€โ”€ utils
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jest.config.js
โ”‚   โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”‚   โ”œโ”€โ”€ src
โ”‚   โ”‚   โ””โ”€โ”€ test
// Jest configuration for api
const jestBase = require('../../jestBaseConfig.js');

module.exports = {
  ...jestBase,
  coverageThreshold: {
    global: {
      statements: 100,
      branches: 100,
      functions: 100,
      lines: 100,
    },
  },
  testEnvironment: 'node',
  setupFiles: [
    require.resolve('./tests-setup/setupEnv.js'),
  ],
};



md5-c6fad6f6722cd06a86f8dde18980d664



// rootDir's package.json
...
"jest": {
    "projects": [
      "<rootDir>/packages/kh-api/jest.config.js",
      "<rootDir>/packages/kh-components/jest.config.js",
      "<rootDir>/packages/kh-utils/jest.config.js"
    ]
}
...

What is the expected behavior?
jest should pick up jest.config.js within each package folder and run the setupFiles

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

jest: v22.1.2
yarn: 1.3.2
OS: macOS High Sierra

Needs Triage

Most helpful comment

check a running project with multi config here https://github.com/entria/entria-fullstack

All 13 comments

@vadivelkindhealth can you please provide a repo with a reproduction so we can debug this?

@thymikee I'm actually having somewhat a similar issue and I have a repo to illustrate the problem. See cra-workspaces.

Just run yarn install at the root (yarn should be at 1.3+) and then yarn run packages:test. I'm seeing some very weird behaviour:

  1. It ignores my projects config and runs tests even in apps/*
  2. It does not properly use the modeNameMapper which tells it where the package is
  3. If I keep the globals for ts-lint in the jest.config.base.js it will complain for each project that it cannot find the given tsConfigFile because instead of looking for it in the project folder it looks for it at the root

In a monorepo you should have all the test configuration in the root. projects as an array of config blocks (previously located in the leaf package jest.config.js) should be sufficient.

@evocateur I'm not sure that always applies. Some of the projects that I'm merging into a monorepo use aliases which are package specific and should not be in the root config.

@vadivelkindhealth in your jest.config.js files you'll need to set rootDir option to point to the root of your monorepo (where the "projects" option exists).

Why is this closed exactly?

The point of having projects support is because those projects probably have different settings. One jest.config.js file at the root doesn't cover a scenario where a project has tests next to source files and another project as a separate tests folder for example. (Angular or Vue front-end and some other back-end services)

I actually stumbled upon this because I want VSCode to launch jest for the currently opened file, but can't because I can't tell jest which configuration file to use for the currently opened project. I can only run jest for all of the projects because that's what the root Jest config has configured.

P.S.: Just found this related open issue, so I'll move the discussion there https://github.com/facebook/jest/issues/5921

@thymikee as much as I respect that solution, I don't think that is the behaviour we're (or maybe just I) is looking for..

IMO; you define the root/base config file, to handle things like transform /[tj]sx?/, and small reporter options etc... Then inside your packages/*/jest.config.js files, you define more granular setup files, enzyme (for React) or other things that is specific to that packages' testing nature.

So you can:

  1. Simply yarn test to test the entire mono repo
  2. Have a uniform reporting mechanism for ci's
  3. Transform scss, tsx, jsx, and babel config all the same way, but be overrideable relative to the package.
  4. Allow for a per package test, with a yarn test --projects fancyProjA

Am I understanding this accurately?

Unfortunately that's not how it works currently, Jest doesn't merge configs. We're planning to overhaul the configuration in Jest 25 so maybe we can rethink this.

โ˜๏ธ that's precisely what I ran into.

A simple example is adding displayName to each project's jest.config.js. It can be observed that those configurations are not picked up.

check a running project with multi config here https://github.com/entria/entria-fullstack

@sibelius Thanks for sharing your repo. I have a question about this: It looks like you are using full config files in the projects. Is it correct that the root jest.config.js does not impact the project's configuration? If so do you have a way of definint something in the root config that applies to all project's jest config?

@RT-TL I think you can follow https://github.com/facebook/jest/issues/3112#issuecomment-398581705 to set up a similar config structure

I have found weird behaviour - having just one value as projects did not work.

jest: v24.9.0

module.exports = {
  projects: [
    '<rootDir>/js/project1/jest.config.js'
  ]
}

as soon as I added second project, all fine.

module.exports = {
  projects: [
    '<rootDir>/js/project1/jest.config.js',
    '<rootDir>/js/project2/jest.config.js'
  ]
}
Was this page helpful?
0 / 5 - 0 ratings