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

Why differences in babel config for test env? #2429

Closed
justin808 opened this issue Jan 8, 2020 · 2 comments
Closed

Why differences in babel config for test env? #2429

justin808 opened this issue Jan 8, 2020 · 2 comments

Comments

@justin808
Copy link
Contributor

justin808 commented Jan 8, 2020

Regarding the example babel config files:

Is this necessary:
https://github.com/rails/webpacker/blob/master/lib/install/examples/react/babel.config.js#L26

and why the setting of targets.node='current' for test env only?

This is definitely related: #416

But I think it's not necessary with newer versions of babel, jest, node, etc.

And simpler is better, generally.

return {
    presets: [
      isTestEnv && [
        '@babel/preset-env',
        {
          targets: {
            node: 'current'
          }
        }
      ],
      (isProductionEnv || isDevelopmentEnv) && [
        '@babel/preset-env',
        {
          forceAllTransforms: true,
          useBuiltIns: 'entry',
          corejs: 3,
          modules: false,
          exclude: ['transform-typeof-symbol']
        }
      ]

2020-01-07_22-09-34

I think just adding this plugin for the test env would do the trick:

      plugins.push('@babel/plugin-transform-modules-commonjs');
@jakeNiemiec
Copy link
Member

jakeNiemiec commented Jan 8, 2020

image

why the setting of targets.node='current' for test env only?

Users may not be running the current release of node. Some temperamental libraries don't yet support new things in current since it's the bleeding edge.

Is this necessary: modules: 'commonjs'

I think so for the following reasons:

  • If not set, I think it defaults to ES modules from webpack (or so I have read). These are not supported in all versions of nodejs (which jest uses to run).
  • Depending on the version, you need to additionally include the --experimental-modules flag

I don't think we will need to worry about this for very long since [email protected] has removed the flag requirement.

@justin808
Copy link
Contributor Author

This issue was well explained by @rossta: Why doesn't Webpacker use my test config when I run Rails tests?
.

In summary, NODE_ENV == 'test' is used to run JS unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants