Jest: Can't find a root directory while resolving a config file path.

Created on 17 Feb 2018  路  8Comments  路  Source: facebook/jest

What is the current behavior?

The test suite fails to run because it can't properly resolve the path.

Config

module.exports = {
  projects: [{
    displayName: 'unit',
    testMatch: ['**/__tests__/unit/**/*.spec.js']
  }]
}

Execution

jest --projects unit

Error: Can't find a root directory while resolving a config file path.
Provided path to resolve: unit
cwd: /Users/code/project

What is the expected behavior?

I expected that the test suite runs with the specified configuration because when I do jest --showConfig it shows my rootDir as /Users/code/project which should properly resolve testMatch: ['**/__tests__/unit/**/*.spec.js'] to '/Users/code/project/__tests__/unit/**/*.spec.js'.

I also tried to use <rootDir> for the testMatch but the same result and it looks like it tries to resolve the unit as a path instead of looking it up in the jest.config.js?

Am I supposed to put a jest.config.js into every folder and then run jest --projects tests/unit? If that is the case then how is the projects option supposed to be used.

Jest, node, yarn and macOS are the latest versions.

Most helpful comment

@elyobo Correct. PR welcome for some way of specifying which of the loaded projects to run 馃檪

All 8 comments

displayName is just for the name, not a way to run it. Although that would be cool 馃檪

--projects on the cli is the same as projects in config, except it can just be paths to different config file, not inline JSON

Thanks, figured that after some testing and ended up putting a config into each directory.

Just to confirm I understand this correctly - there's no way to specify the project to run via the CLI if it's configured using a single config file (e.g. in package.json, jest.config.js etc), but you can specify the test to run (but run all projects by default) by making a separate config file in each project and then referring to those in the main config file, e.g.

package.json snippet

  "jest": {
    "projects": [
      "<rootDir>/jest-test.config.js",
      "<rootDir>/jest-eslint.config.js"
    ]
  }

jest-test.config.js

'use strict';

module.exports = {
  displayName: 'test',
  testPathIgnorePatterns: [
    '/node_modules/',
    '/__tests__/fixtures/',
  ],
};

jest-eslint.config.js

'use strict';

module.exports = {
  runner: 'jest-runner-eslint',
  displayName: 'lint',
  testMatch: ['<rootDir>/**/*.js'],
  testPathIgnorePatterns: [
    '/node_modules/',
  ],
};

It would be nice to avoid scattering the config around like this and still be able to use --projects to indicate the project(s) to run, e.g. by matching the displayName.

@elyobo Correct. PR welcome for some way of specifying which of the loaded projects to run 馃檪

I'll see if I can find some time! I went ahead and split out the config files, which works fine, it just feels like clutter :D

Would be great to avoid duplication of identical configs in monorepos. Something like a pathMatch option for projects: [] so similar sub projects can be grouped together.

Thanks for the clarification. Could you please add that same clarification to the docs? Right now, the explanation of --projects is so minimal that I can't tell if it's confusingly ambiguous or just wrong.

Wanna send a PR for that? :)

Was this page helpful?
0 / 5 - 0 ratings