Microbundle: Provide a Jest Transform

Created on 29 Jan 2020  路  6Comments  路  Source: developit/microbundle

It may be convenient to expose something to consumers that will make it easier to use Jest - something like the default babel options (presets, plugins, etc.) or a transform module (see something like https://github.com/ActuallyACat/jest-esm-transformer).

This makes using syntax like import/export much easier for folks who use this amazing library for bundling and Jest for tests.

Thank you!

enhancement good first issue help wanted

Most helpful comment

@therealparmesh great suggestion. I see that Jest can already do this when Babel is installed, though because Microbundle abstract Babel Jest likely doesn't detect it. So I guess you're saying it would be nice if there was a Microbundle-provided option here?

If so, I agree!

All 6 comments

@therealparmesh great suggestion. I see that Jest can already do this when Babel is installed, though because Microbundle abstract Babel Jest likely doesn't detect it. So I guess you're saying it would be nice if there was a Microbundle-provided option here?

If so, I agree!

Precisely what I meant!

FWIW, my current workaround is to have my Jest tests import from dist (You can see it here: https://github.com/therealparmesh/object-to-formdata/blob/b0c0be74a3e510ea8be2efa56f85e4e21ba3afa7/src/index.test.js#L1). This works well - even with features like code coverage (because of the source maps that point to the src).

Any feedback on this? 馃槃

microbundle could save the configuration to the project root with a "This is autogenerated. Don't change it."

If microbundle would come with a "public" Babel config, it would also be possible to put a eslint config to the root with just a extend property.

If you're ending up here from Google, here's an approach that worked for me:

  • Create a babel.config.js file at the project's root:
// Microbundle uses its own Babel config, so we need to make sure we use this only for tests
module.exports =
  process.env.NODE_ENV === 'test'
    ? {
        presets: [
          [
            '@babel/preset-env',
            {
              targets: {
                node: 'current',
              },
            },
            '@babel/preset-react',
          ],
          '@babel/preset-typescript',
        ],
        plugins: [
          [
            '@babel/plugin-transform-runtime',
            {
              regenerator: true,
            },
          ],
          '@babel/plugin-proposal-class-properties',
          '@babel/plugin-proposal-export-default-from',
          ['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
        ],
      }
    : {}
  • You'll have to install those plugins/presets with npm install --save-dev (note that I'm using TypeScript; you might want to remove @babel/preset-typescript if you're not)
  • Make sure you add NODE_ENV=test to your test scripts, for instance:
    "test": "NODE_ENV=test jest --coverage",
    "test:no-coverage": "NODE_ENV=test jest",
    "test:watch": "NODE_ENV=test jest --coverage --watch",
  • If you have a transform option in your Jest configuration, you might need to remove it so that Jest uses the babel-jest transformer.

Specifying a babel config for jest is the proper way to do it :+1:

Closing, because shipping microbundle with a jest transform is out of scope for this project. This project's main purpose is to bundle up javascript packages for npm distribution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

developerdizzle picture developerdizzle  路  5Comments

breadadams picture breadadams  路  3Comments

rzkhosroshahi picture rzkhosroshahi  路  5Comments

jlkiri picture jlkiri  路  4Comments

dmitrykurmanov picture dmitrykurmanov  路  3Comments