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

Add Babel 7 support to babel-preset-jest #6126

Closed
edmorley opened this issue May 3, 2018 · 2 comments · Fixed by #7203
Closed

Add Babel 7 support to babel-preset-jest #6126

edmorley opened this issue May 3, 2018 · 2 comments · Fixed by #7203

Comments

@edmorley
Copy link
Contributor

edmorley commented May 3, 2018

Hi :-)

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
Attempting to use babel-preset-jest with Babel 7 beta 46 roughly like so:

const babelConfig = {
  // ...
  presets: [require.resolve('babel-preset-jest')],
  plugins: [
    // ...
  ]
}

(sorry for not a full example; it's deeply intertwined with Neutrino and the cause is clear from code-inspection below)

...results in:

    Plugin/Preset files are not allowed to export objects, only functions. In C:\Users\Ed\src\neutrino-dev\node_modules\babel-preset-jest\index.js

      at createDescriptor (../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:181:11)
      at ../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:106:12
          at Array.map (<anonymous>)
      at createDescriptors (../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:105:27)
      at createPresetDescriptors (../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:97:10)
      at ../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:83:14
      at cachedFunction (../neutrino-dev/node_modules/@babel/core/lib/config/caching.js:42:17)
      at presets (../neutrino-dev/node_modules/@babel/core/lib/config/config-descriptors.js:28:68)
      at mergeChainOpts (../neutrino-dev/node_modules/@babel/core/lib/config/config-chain.js:371:68)
      at ../neutrino-dev/node_modules/@babel/core/lib/config/config-chain.js:324:7

This is because as of babel/babel#6494, presets must export something like:

exports.default = function () {
  return {
    plugins: [
      // ...
    ]
  };
};

...whereas babel-preset-jest exports an object:
https://github.com/facebook/jest/blob/da60c7811bb808c474e7ef887884e5d00cffa115/packages/babel-preset-jest/index.js#L8-L14

babel-jest appears to work around this by importing the contents of the preset and .concat()ing it directly:
https://github.com/facebook/jest/blob/cf2b9e80a0b099c2f861660fd9cf589848106fa1/packages/babel-jest/src/index.js#L77

What is the expected behavior?

That the .concat() workaround not be required when using babel-preset-jest directly (which is required for use-cases like those in #1468).

Please provide your exact Jest configuration
The config we're using under Babel 6 (the Babel 7 branch is a local WIP; but master should give the context as to use case) is here:
https://github.com/mozilla-neutrino/neutrino-dev/blob/c725322e0e7fe62783cc3b6d78b46be1793c51a7/packages/jest/src/index.js
https://github.com/mozilla-neutrino/neutrino-dev/blob/c725322e0e7fe62783cc3b6d78b46be1793c51a7/packages/jest/src/transformer.js

Run npx envinfo --preset jest in your project directory and paste the
results here

I use yarn rather than npm so don't have npx installed. Perhaps it would be good to update the template with the yarn equivalents?

edmorley added a commit to neutrinojs/neutrino that referenced this issue May 3, 2018
Notable changes:
* All official packages have moved under the `@babel/` namespace and in
  some cases further renamed, so package names adjusted accordingly.
* `@babel/preset-env` now includes `@babel/plugin-transform-spread` and
  `@babel/plugin-transform-classes`, which are the Babel 7 renames of
  `babel-plugin-transform-object-rest-spread` and
  `babel-plugin-transform-es2015-classes`.
* `@babel/preset-env`'s `useBuiltIns: true` mode has been renamed to
  `useBuiltIns: 'entry'` - which is equivalent.
* `@babel/preset-react` now has `development` and `useBuiltIns` options,
  which we set appropriately:
  https://github.com/babel/babel/tree/v7.0.0-beta.46/packages/babel-preset-react#options
* `babel-plugin-dynamic-import-node` is no longer required by
  `@neutrinojs/library` for target `node`, since webpack converts the
  dynamic import to a require itself.
* `@neutrinojs/jest` required more substantial changes since:
  - the custom transformer used the now removed `canCompile()` from
    Babel's API.
  - `babel-preset-jest` is not fully compatible with Babel 7
    (jestjs/jest#6126).
* Several packages now have a peer dependency on `@babel/core`, so it's
  been added where necessary.
* `babel-loader` has been updated to v8 for Babel 7 compatibility.
* `@neutrinojs/vue`'s `babel-preset-vue` dependency doesn't appear to
  be used, but has been left as is pending #836.

Closes #316.
@SimenB
Copy link
Member

SimenB commented May 5, 2018

PR welcome! A function should work fine for Babel 6 as well.

As a workaround you can create your own file which does require and wraps it in a function, then do require.resolve on that one instead.

edmorley added a commit to neutrinojs/neutrino that referenced this issue May 6, 2018
Notable changes:
* All official packages have moved under the `@babel/` namespace and in
  some cases further renamed, so package names adjusted accordingly.
* `@babel/preset-env` now includes `@babel/plugin-transform-spread` and
  `@babel/plugin-transform-classes`, which are the Babel 7 renames of
  `babel-plugin-transform-object-rest-spread` and
  `babel-plugin-transform-es2015-classes`.
* `@babel/preset-env`'s `useBuiltIns: true` mode has been renamed to
  `useBuiltIns: 'entry'` - which is equivalent.
* `@babel/preset-react` now has `development` and `useBuiltIns` options,
  which we set appropriately:
  https://github.com/babel/babel/tree/v7.0.0-beta.46/packages/babel-preset-react#options
* `babel-plugin-dynamic-import-node` is no longer required by
  `@neutrinojs/library` for target `node`, since webpack converts the
  dynamic import to a require itself.
* `@neutrinojs/jest` required more substantial changes since:
  - the custom transformer used the now removed `canCompile()` from
    Babel's API.
  - `babel-preset-jest` is not fully compatible with Babel 7
    (jestjs/jest#6126).
* Several packages now have a peer dependency on `@babel/core`, so it's
  been added where necessary.
* `babel-loader` has been updated to v8 for Babel 7 compatibility.
* `@neutrinojs/vue`'s `babel-preset-vue` dependency doesn't appear to
  be used, but has been left as is pending #836.

Closes #316.
edmorley added a commit to neutrinojs/neutrino that referenced this issue May 7, 2018
Notable changes:
* All official packages have moved under the `@babel/` namespace and in
  some cases further renamed, so package names adjusted accordingly.
* `@babel/preset-env` now includes `@babel/plugin-transform-spread` and
  `@babel/plugin-transform-classes`, which are the Babel 7 renames of
  `babel-plugin-transform-object-rest-spread` and
  `babel-plugin-transform-es2015-classes`.
* `@babel/preset-env`'s `useBuiltIns: true` mode has been renamed to
  `useBuiltIns: 'entry'` - which is equivalent.
* `@babel/preset-react` now has `development` and `useBuiltIns` options,
  which we set appropriately:
  https://github.com/babel/babel/tree/v7.0.0-beta.46/packages/babel-preset-react#options
* `babel-plugin-dynamic-import-node` is no longer required by
  `@neutrinojs/library` for target `node`, since webpack converts the
  dynamic import to a require itself.
* `@neutrinojs/jest` required more substantial changes since:
  - the custom transformer used the now removed `canCompile()` from
    Babel's API.
  - `babel-preset-jest` is not fully compatible with Babel 7
    (jestjs/jest#6126).
* Several packages now have a peer dependency on `@babel/core`, so it's
  been added where necessary.
* `babel-loader` has been updated to v8 for Babel 7 compatibility.
* `@neutrinojs/vue`'s `babel-preset-vue` dependency doesn't appear to
  be used, but has been left as is pending #836.

Closes #316.
@SimenB SimenB added this to the Jest 24 milestone Oct 16, 2018
@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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants