Jest: Use of --projects option breaking multiple project config, even crashing

Created on 30 May 2018  ·  2Comments  ·  Source: facebook/jest

🐛 Bug Report

TL;DR: Use of the --projects CLI option is somehow making Jest ignore my multi-project config.

I've added jest-pytest to my Jest setup. To keep my existing JS tests running, I've created jest-pytest.json:

{
    "projects": [
        {
            "displayName": "ui",
            "testPathIgnorePatterns": [ "utils" ],
            "rootDir": "/Users/yoz/Work/peops"
        },
        {
            "displayName": "pytest",
            "moduleFileExtensions": [ "py" ],
            "rootDir": "/Users/yoz/Work/peops",
            "runner": "jest-pytest",
            "testPathIgnorePatterns": [ "snap_.*\\.py" ],
            "testMatch": [
                "**/?(*_)test?(s).py",
                "**/test_*.py"
            ]
        }
    ]
}

Here's what happens when I run jest with the default config:

MacBook-Pro-2:peops yoz$ yarn run jest --listTests
yarn run v1.6.0
$ /Users/yoz/Work/peops/node_modules/.bin/jest --listTests
/Users/yoz/Work/peops/ui/utilities/__tests__/vesting.test.js
/Users/yoz/Work/peops/ui/utilities/__tests__/utils.test.js
✨  Done in 3.15s.
MacBook-Pro-2:peops yoz$ npx jest --listTests
/Users/yoz/Work/peops/ui/utilities/__tests__/vesting.test.js
/Users/yoz/Work/peops/ui/utilities/__tests__/utils.test.js

✅ When pointing to the above config file, the right tests are included & excluded: ✅

MacBook-Pro-2:peops yoz$ npx jest --config=jest-pytest.json --listTests
/Users/yoz/Work/peops/comp/tests.py
/Users/yoz/Work/peops/dash/tests.py
/Users/yoz/Work/peops/core/tests.py
/Users/yoz/Work/peops/data/importing_tests.py
/Users/yoz/Work/peops/ui/utilities/__tests__/vesting.test.js
/Users/yoz/Work/peops/comp/model_tests.py
/Users/yoz/Work/peops/comp/integration_tests.py
/Users/yoz/Work/peops/data/tests.py

🔴 However, when specifying a particular project display name to use, it ignores the given config: 🔴

MacBook-Pro-2:peops yoz$ npx jest --config=jest-pytest.json --projects ui --listTests
/Users/yoz/Work/peops/ui/utilities/__tests__/vesting.test.js
/Users/yoz/Work/peops/ui/utilities/__tests__/utils.test.js
MacBook-Pro-2:peops yoz$ npx jest --config=jest-pytest.json --projects pytest --listTests
/Users/yoz/Work/peops/ui/utilities/__tests__/vesting.test.js
/Users/yoz/Work/peops/ui/utilities/__tests__/utils.test.js

🔴 And when specifying two projects, it goes bang: 🔴

MacBook-Pro-2:peops yoz$ npx jest --config=jest-pytest.json --projects pytest ui --listTests
Error: Can't find a root directory while resolving a config file path.
Provided path to resolve: pytest
cwd: /Users/yoz/Work/peops
    at exports.default (/Users/yoz/Work/peops/node_modules/jest-config/build/resolve_config_path.js:65:11)
    at readConfig (/Users/yoz/Work/peops/node_modules/jest-config/build/index.js:165:67)
    at projects.filter.map.root (/Users/yoz/Work/peops/node_modules/jest-cli/build/cli/index.js:489:60)
    at Array.map (<anonymous>)
    at getConfigs (/Users/yoz/Work/peops/node_modules/jest-cli/build/cli/index.js:488:8)
    at /Users/yoz/Work/peops/node_modules/jest-cli/build/cli/index.js:238:23
    at Generator.next (<anonymous>)
    at step (/Users/yoz/Work/peops/node_modules/jest-cli/build/cli/index.js:194:30)
    at /Users/yoz/Work/peops/node_modules/jest-cli/build/cli/index.js:213:14
    at new Promise (<anonymous>)

I tried running jest --clearCache, didn't help.

Also ran with --debug and found that the globalConfig.projects object goes missing when --projects is specified. (See this gist with the outputs.)

To Reproduce

Don't have time to create a proper repro right now, but I can work on it later if you're not able to repro separately.

Expected behavior

--projects should work with the specified projects config and only run the projects with the display names given to the option.

Run npx envinfo --preset jest

Paste the results here:

MacBook-Pro-2:peops yoz$ npx envinfo --preset jest

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 10.1.0
    Yarn: 1.6.0
    npm: 5.6.0
  npmPackages:
    jest:
      wanted: ^23.1.0
      installed: 23.1.0

Most helpful comment

What @skidding says is correct. Having a way to filter projects by name would be awesome.

You can use https://github.com/rogeliog/jest-watch-select-projects for watch mode, but we really should have a CLI flag for it as well. Might simplify that plugin of Jest natively supports it, as it'd just have to hook into that instead of doing its own logic, /cc @rogeliog

All 2 comments

If I understood correctly from #5595, the projects argument doesn't filter your tests based on project name(s), but redefines the projects (that you already defined in the Jest config). It makes sense since the CLI options in Jest are a mirror of the config options, or viceversa.

I also wish there were a way to run Jest for specific projects defined in config.

What @skidding says is correct. Having a way to filter projects by name would be awesome.

You can use https://github.com/rogeliog/jest-watch-select-projects for watch mode, but we really should have a CLI flag for it as well. Might simplify that plugin of Jest natively supports it, as it'd just have to hook into that instead of doing its own logic, /cc @rogeliog

Was this page helpful?
0 / 5 - 0 ratings