Skip to content

[babel-jest] Feature: support expanded .babelrc.js api #5324

Closed
@simonbuchan

Description

@simonbuchan

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

What is the current behavior?
.babelrc.js export is flattened to JSON

This effectively ignores any features you would use .babelrc.js for, and shows up as things like unexpected token 'import' when the function export is treated as an empty config, or overrides are ignored because RegExp tests are stripped, etc..., so no transforms are run.

What is the expected behavior?

In short, babel-jest looks like it should be using the new loadOptions() API in @babel/core with at least { babelrc: true, filename, envName: 'test' }, though it is not documented yet 😟

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

// package.json
{
  "private": true,
  "devDependencies": {
    "@babel/core": "7.0.0-beta.37",
    "@babel/preset-env": "7.0.0-beta.37",
    "@babel/preset-flow": "7.0.0-beta.37",
    "@babel/preset-react": "7.0.0-beta.37",
    "@babel/preset-stage-3": "7.0.0-beta.37",
    "@babel/preset-typescript": "7.0.0-beta.37",
    "babel-jest": "22.0.6",
    "jest": "22.0.6"
  }
}

// .babelrc.js example
module.exports = api => {
  // Can also use arbitrary tests with `const val = api.cache(() => expr)`
  const env = api.env();

  // Jest needs CommonJS, but otherwise we want webpack to see ES modules.
  const modules = env === 'test' ? 'commonjs' : false;

  const targets = env === 'test'
    ? { node: 'current' }
    : { browsers: ['> 5%', 'IE 11'] };

  const presets = [
    ['@babel/env', { modules, useBuiltIns: 'usage', targets }],
    '@babel/stage-3',
  ];

  return {
    overrides: [
      { test: /\.jsx?$/, presets: [...presets, '@babel/flow', '@babel/react'] },
      { test: /\.ts$/, presets: [...presets, '@babel/typescript'] },
      { test: /\.tsx$/, presets: [...presets, '@babel/typescript', '@babel/react'] },
    ],
  };
};

Example loadOptions() usage:

> node -p "require('@babel/core').loadOptions({ babelrc: true, filename: 'src/index.js', envName: 'test' })"
{ plugins:
   [ Plugin {
       key: 'C:\\code\\skilitics\\prototypes\\layout\\node_modules\\@babel\\preset-react\\lib\\index.js$0',
       manipulateOptions: [Function: manipulateOptions],
       post: undefined,
       pre: undefined,
       visitor: [Object],
       parserOverride: undefined,
       generatorOverride: undefined,
       options: [Object] },
<snip>
     Plugin {
       key: 'use-built-ins',
       manipulateOptions: undefined,
       post: [Function: post],
       pre: [Function: pre],
       visitor: [Object],
       parserOverride: undefined,
       generatorOverride: undefined,
       options: [Object] } ],
  overrides:
   [ { test: /\.jsx?$/, presets: [Object] },
     { test: /\.ts$/, presets: [Object] },
     { test: /\.tsx$/, presets: [Object] } ],
  test: /\.jsx?$/,
  babelrc: false,
  filename: 'src/index.js',
  envName: 'test',
  presets: [],
  passPerPreset: false,
  cwd: 'C:\\code\\skilitics\\prototypes\\layout' }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions