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

Automatic use of .babelrc causes TypeError: Cannot read property 'globals' of undefined #4954

Closed
onury opened this issue Nov 24, 2017 · 9 comments

Comments

@onury
Copy link

onury commented Nov 24, 2017

Do you want to request a feature or report a bug?
Bug.

What is the current behavior?
As @cpojer mentions here, Jest uses .babelrc (or babel property in package.json) only because it exists there. Even in cases where the configuration and usage does not even relate to or trigger babel.

jest --coverage throws error below:

Test suite failed to run

    TypeError: Cannot read property 'globals' of undefined

      at _default (node_modules/@babel/plugin-transform-modules-umd/lib/index.js:16:24)
      at Function.memoisePluginContainer (node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)
      at Function.normalisePlugin (node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
      at Array.map (native)
      at Function.normalisePlugins (node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)

If I remove the babel section from package.json, jest will work as expected.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

  • Just add jest and babel configurations in your package.json
  • Don't use any babel transformer for jest. (no mention of babel in jest config)
  • Use babel separately to build (you don't even have to use babel.. it's irrelevant to this bug)
  • Just run jest --coverage
    Above error will occur.

What is the expected behavior?
Do not automatically use .babelrc or babel property in package.json.

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

Below is part of my package.json with jest and babel properties:

...
  "jest": {
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/dist",
      "<rootDir>/test"
    ],
    "testRegex": "(/test/.*|(\\.|/)(test|spec))\\.js?$",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/.coverage/"
    ],
    "collectCoverageFrom": [
      "dist/lib.js"
    ],
    "coverageDirectory": "./test/.coverage",
    "mapCoverage": true
  },
  "babel": {
      "test": {
        "presets": [
          "@babel/preset-env"
        ],
        "plugins": [
          "@babel/plugin-transform-modules-umd"
        ]
      }
    }
  },
  "scripts": {
    "build:test": "BABEL_ENV=test babel src --out-file dist/lib.js",
    "cover": "yarn build:test && jest --coverage --verbose --no-cache",
    ...
  },
...

Yarn v1.3.2
Node v6.11.2
Jest CLI v21.2.1
macOS 10.13.1
@loganfsmyth
Copy link

The most recent beta of babel-jest includes support for Babel 7. See https://github.com/facebook/jest/tree/master/packages/babel-jest#usage with the note about Babel 7 usage. You have to install babel-core@^7.0.0-0 along with the most recent beta of babel-jest and it should work.

@onury
Copy link
Author

onury commented Nov 25, 2017

@loganfsmyth I'm using @babel/core and @babel/cli both 7.0.0-beta.32 and they work ok.

But the problem here is I don't want to use babel and jest in conjunction. I don't need another tool for it. I use them separately in the same project. And it's clearly a bug that jest auto-uses babel config where ever it sees it.

@loganfsmyth
Copy link

The docs for Babel state:

Note: babel-jest is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the transform configuration option:
// package.json

{
  "jest": {
    "transform": {}
  }
}

@onury
Copy link
Author

onury commented Nov 25, 2017

Ah this works (around)! :) Thanks @loganfsmyth for pointing that out.
I'm sure this behavior will be removed eventually, though... logically..

@SimenB
Copy link
Member

SimenB commented Nov 25, 2017

Closing as this is working as intended, and the workaround in the docs works.

Thanks for chiming in @loganfsmyth!

I'm sure this behavior will be removed eventually, though... logically..

Why do you think that?

@SimenB SimenB closed this as completed Nov 25, 2017
@onury
Copy link
Author

onury commented Nov 25, 2017

@SimenB setting an empty object to disable an unrelated, sub-component is not right, not elegant. The user should opt-in to enable such a behavior. Otherwise, it still feels buggy to me.

When you look at the configuration, you feel you can/should just delete transform there:

{
  "jest": {
    "transform": {}
  }
}

@loganfsmyth
Copy link

Why do you think that?

I'll say I think it's weird. I didn't realize babel-jest was installed automatically until this issue was referenced. It seems like people should have to opt in to integrating Babel into another tool.

@timarandras
Copy link

Thanks for this workaround, it worked for me.

@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
None yet
Projects
None yet
Development

No branches or pull requests

4 participants