Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--config [JSON] + projects doesn't seem to work #7415

Closed
benmvp opened this issue Nov 27, 2018 · 8 comments · Fixed by #11307
Closed

--config [JSON] + projects doesn't seem to work #7415

benmvp opened this issue Nov 27, 2018 · 8 comments · Fixed by #11307
Assignees

Comments

@benmvp
Copy link

benmvp commented Nov 27, 2018

🐛 Bug Report

When passing serialized JSON for the --config option that contains projects, I get the following error:

Error: Jest: Cannot use configuration as an object without a file path.
    at readConfig (~/github/benmvp/benmvp-cli/node_modules/jest-config/build/index.js:139:13)
    at projects.filter.map.root ~/github/benmvp/benmvp-cli/node_modules/jest-cli/build/cli/index.js:487:62)
    ....

The parsed JSON looks like:

{
    "projects": [
        {
            "moduleFileExtensions": ["ts", "js"],
            "transform": {
                "^.+\\.(ts|js)$": "babel-jest"
            },
            "testMatch": [
                "<rootDir>/src/**/*.ts"
            ],
            "runner": "jest-runner-tsc",
            "displayName": "typescript"
        },
        {
            "moduleFileExtensions": ["ts", "js"],
            "transform": {
                "^.+\\.(ts|js)$": "babel-jest"
            },
            "testMatch": [
                "<rootDir>/src/**/*.spec.ts"
            ],
            "displayName": "test"
        }
    ],
    "rootDir": "."
}

To Reproduce

Just run:

npx jest --config '{"projects":[{"moduleFileExtensions":["ts","js"],"transform":{"^.+\\.(ts|js)$":"babel-jest"},"testMatch":["<rootDir>/src/**/*.ts"],"runner":"jest-runner-tsc","displayName":"typescript"},{"moduleFileExtensions":["ts","js"],"transform":{"^.+\\.(ts|js)$":"babel-jest"},"testMatch":["<rootDir>/src/**/*.spec.ts"],"displayName":"test"}],"rootDir":"."}'

Expected behavior

I expect to be able to run the projects just like if they were defined in a config file (which does work).

Link to repl or repo (highly encouraged)

See repro command. Happy to contribute a PR, I just don't know what to fix. 😄

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Binaries:
    Node: 10.12.0 - ~/.nvm/versions/node/v10.12.0/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0

Debugging

1/ I tried moving the JSON back to a .js file and it works (my current workaround). I need the contents to be dynamic, which is why I want to pass JSON

2/ I did a whole bunch of code spelunking to try to get it to work. I narrowed down the problem to readConfigs in jest-config. It seems that after successfully parsing the JSON, it maps over each project to readConfig on each (passing configPath). The problem is that configPath is null so readConfig complains. And the reason configPath is null is because when the JSON was originally parsed (also by readConfig), I guess there is no configPath to set so it remains null. That's as far as I got since I don't understand the why of the code.

@rickhanlonii
Copy link
Member

Hey @benmvp thanks for filing! Confirmed - I'll take a look

@rickhanlonii rickhanlonii self-assigned this Nov 27, 2018
@benmvp
Copy link
Author

benmvp commented Nov 27, 2018

Awesome! Thanks @rickhanlonii! Lemme know if there's any way I can help

@rickhanlonii
Copy link
Member

Hey @benmvp, I took a look at this and it looks like the projects need to be specified as separate cli options as npx jest --projects p1 p2 --config c (see comment here)

Can you try:

npx jest --projects '{"moduleFileExtensions":["ts","js"],"transform":{"^.+\\.(ts|js)$":"babel-jest"},"testMatch":["<rootDir>/src/**/*.ts"],"runner":"jest-runner-tsc","displayName":"typescript"}' '{"moduleFileExtensions":["ts","js"],"transform":{"^.+\\.(ts|js)$":"babel-jest"},"testMatch":["<rootDir>/src/**/*.spec.ts"],"displayName":"test"}' --config '{"rootDir":"."}'

@benmvp
Copy link
Author

benmvp commented Nov 28, 2018

Hi @rickhanlonii thanks for getting back to me so quickly! So I thought I had a spark of brilliance yesterday and I tried moving the projects into JSON args but it doesn't work. They don't get parsed.

When I did it, I tried it without the --config argument and got:

Error: Can't find a root directory while resolving a config file path.
Provided path to resolve: {"moduleFileExtensions":["ts","js"],"transform":{"^.+\\.(ts|js)$":"babel-jest"},"testMatch":["<rootDir>/src/**/*.ts"],"runner":"jest-runner-tsc","displayName":"typescript"}
cwd: ~/github/benmvp/benmvp-cli
    at exports.default (~/github/benmvp/benmvp-cli/node_modules/jest-config/build/resolveConfigPath.js:65:11)
    at readConfig (~/github/benmvp/benmvp-cli/node_modules/jest-config/build/index.js:166:80)

When I run it with --config as you suggested, it runs as if the --projects arg is not there. In short, is just running the default jest.

I've only gotten the --projects arg to work if it's a path to a file

@RobertBroersma
Copy link

I'm running into the same issue as @benmvp with the configPath always being null on this line: https://github.com/facebook/jest/blob/master/packages/jest-config/src/index.ts#L333

I'm in the same boat where I don't know exactly what this configPath means, but replacing that line with process.cwd() seems to fix it for me.

I'd be happy to send a PR, but I'm not 100% sure what should be the fix 😅

Additional context
In RedwoodJS you can configure multiple sides in your app that require different Jest configs (for frontend, backend, React Native, more). I'm trying to dynamically construct a project per side.

@hupe1980
Copy link

I have the same problem. When I use a serialized JSON for the --config option that contains projects, I also get the following error:

Error: Jest: Cannot use configuration as an object without a file path.

@mike-czarnota
Copy link

Hello, how is the progress? I'm specifying the config in the jest.config.js and still get this error 😿

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants