Jest: Add Babel 7 support to babel-preset-jest

Created on 3 May 2018  路  1Comment  路  Source: facebook/jest

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?

Help Wanted good first issue

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hramos picture hramos  路  3Comments

StephanBijzitter picture StephanBijzitter  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

ticky picture ticky  路  3Comments