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

Babel config not loading correctly for --projects. #7771

Closed
bradfordlemley opened this issue Jan 31, 2019 · 6 comments
Closed

Babel config not loading correctly for --projects. #7771

bradfordlemley opened this issue Jan 31, 2019 · 6 comments
Labels

Comments

@bradfordlemley
Copy link
Contributor

🐛 Bug Report

Babel config is not loaded correctly for --projects, e.g.

/mono
  /packages
    /pkg1
      .babelrc.js

jest --projects packages/pkg1 <-- fails

cd packages/pkg1 && jest <-- passes

To Reproduce

The examples/react in the jest monorepo itself actually does exhibit this bug.
The bug is hidden because of babelrcRoots: ['examples/*'] in the higher-level babel.config.js.

Expected behavior

Behavior should be the same as running jest in the project's dir, e.g. cd examples/react && jest

Details to reproduce

In this jest repo itself....

  1. Remove babelrcRoots: ['examples/*'] in top-level babel.config.js.
  2. Run yarn jest --projects examples/react/ <-- fails with message below
  3. Run pushd examples/react && yarn jest <-- passes

Failure message

 FAIL  examples/react/__tests__/CheckboxWithLabel-test.js
  ● Test suite failed to run

    SyntaxError: jest/examples/react/__tests__/CheckboxWithLabel-test.js: Unexpected token, expected "," (11:23)

       9 |   // Render a checkbox with label in the document
      10 |   const checkbox = TestUtils.renderIntoDocument(
    > 11 |     <CheckboxWithLabel labelOn="On" labelOff="Off" />
         |                        ^
      12 |   );
      13 |
      14 |   const checkboxNode = ReactDOM.findDOMNode(checkbox);

      at _class.raise (../../node_modules/@babel/parser/lib/index.js:4051:15)

Analysis

The failure occurs because the babel config is not loaded with the proper cwd...the top-level cwd is used, instead of the project's rootDir. See here and here.
Also, when jest is loading configs, cwd in the project configs are set to the top-level (original) cwd here...not sure if that's the intention, seems like they should be set to each project's rootDir.

@bradfordlemley
Copy link
Contributor Author

After some more analysis...
Jest runs --projects with cwd at top-level, instead of changing cwd to each project. That makes running all projects together at top level behave differently than running each individually, at least with regard to babel, because .babelrc.js in individual projects apply when running the project individually, but don't apply when the project is run at top level via --projects. The behavior is inherited from babel's config loading behavior.
The .babelrc.js files will apply if the project dirs are added to babelrcRoots in the top-level babel.config.js, like how it is done in this repo.

I think it is confusing that the behavior is different, but I haven't convinced myself that it's wrong, so I'll close this out. Thanks!

@SimenB
Copy link
Member

SimenB commented Feb 1, 2019

I wonder if we should tell babel that cwd is rootDir instead of cwd... Ref #7438 (comment)

We might get away with requiring people to set e.g. rootDir: '../' if they put their config outside of what is considered the root of the config, though. It's needed now for a bunch of different stuff anyways (E.g. React Native expects <rootDir>/node_modules to be a thing that exists.)

/cc @thymikee @rickhanlonii @rubennorte thoughts?

@bradfordlemley
Copy link
Contributor Author

I think the question is: what is the intention of --projects? Should it be exactly the same as running each individually? (For me, that was my expectation, I simply want to run all my projects' tests in the same jest instance.) Or should it behave like babel, where the behavior is different because cwd is different? (For me, that's confusing, but maybe some people expect that.)

Another consideration is a case like below which I would expect to "just work" when running --projects or individually.

/root
  babel.config.js  (or .babelrc.js?)
  globalSetup.js
  /prj-1
    .babelrc.js
  /prj-2

What are the proper babel programmatic options for files outside of project dir? Maybe {cwd: prj.rootDir , rootMode: 'upwards-optional'} would work for most cases, with overrides possible via #7288? Babel docs suggest using upward for monorepos, but it makes more sense to me to fall back to babelRoot == prj.rootDir if there isn't a babel.config.js. (I think this would be msotly backwards-compatible with current behavior, because, if there is a babel.config.js, then it would apply and it would need to contain .babelrc.js locations in babelrcRoots for them to apply.)

@SimenB
Copy link
Member

SimenB commented Feb 1, 2019

It was, AFAIK, designed to run different projects in the same instance - e.g. react and jest at the same time. It was not designed (although I think way more used as) a way to apply different jest config to different tests/files within the same project. So the answer to "Should it be exactly the same as running each individually?" is "yes".

@bradfordlemley
Copy link
Contributor Author

In that case, I'm convinced that the current behavior isn't correct.
Did a little more digging and found that #7359 already covers the issue and with similar analysis, so I'll leave this one closed in favor of that one. Thanks!

@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants