babel-utils/babel-plugin-tester

plugins from babelOptions are applied differently in fixtures vs tests

eps1lon opened this issue ยท 3 comments

  • babel-plugin-tester version: 9.0.1
  • node version: 14.3.0
  • npm (or yarn) version: 1.22.4

Relevant code or config

import pluginTester from 'babel-plugin-tester/pure';
import * as path from 'path';
import plugin from 'babel-plugin-macros';

pluginTester({
  plugin,
  babelOptions: {
    plugins: [
      [
        'babel-plugin-module-resolver',
        {
          alias: {
            '@material-ui/utils': path.resolve(__dirname, '../src'),
          },
        },
      ],
    ],
  },
  filename: __filename,
  fixtures: path.resolve(__dirname, './__fixtures__'),
  tests: [
    {
      title: 'can throw on missing error codes',
      error: /unknown: Missing error code for message 'missing'. Did you forget to run `yarn extract-errors` first?/,
      code: `
        import MuiError from '@material-ui/utils/MuiError.macro';

        throw new MuiError('missing');`,
      pluginOptions: {
        muiError: {
          missingError: 'throw',
        },
      },
    },
  ],
});

What you did:

  • use babel-plugin-module-resolver in babelOptions.plugins

What happened:

  • Tests in tests pass since babel-plugin-module-resolver is applied before the plugin.

  • Tests in fixtures fail because babel-plugin-module-resolver is not applied before plugin

    error message
    /home/eps1lon/Development/projects/mui/fork/packages/material-ui-utils/test/__fixtures__/literal/code.js: Cannot find module '@material-ui/utils/MuiError.macro' from '/home/eps1lon/Development/projects/mui/fork/packages/material-ui-utils/test/__fixtures__/literal'
    

Reproduction repository:

  1. clone (checkout this specific commit) https://github.com/eps1lon/material-ui/tree/1ceb45f2f8ec509885fd68285f2ad90f8f1a4e44
  2. yarn
  3. yarn cross-env NODE_ENV=test mocha packages/material-ui-utils/test/MuiError.macro.test.js --watch

Problem description: I want to write the test that mirror actual usage. For babel macros I would need to use a different import source than what is actually used.

Suggested solution:

  1. Always apply babelOptions.plugins first
  2. Always apply babelOptions.plugins last but add an option so that we can configure it (either flag "applyFirst" or custom merger function)

Hi @eps1lon ๐Ÿ‘‹

I'm not 100% certain, but I think option 1 is probably best. ๐Ÿ‘

:tada: This issue has been resolved in version 11.0.0-canary.1 :tada:

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

:tada: This issue has been resolved in version 11.0.0 :tada:

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€