babel-plugin-tester icon indicating copy to clipboard operation
babel-plugin-tester copied to clipboard

plugins from babelOptions are applied differently in fixtures vs tests

Open eps1lon opened this issue 5 years ago • 1 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)

eps1lon avatar May 26 '20 11:05 eps1lon

Hi @eps1lon 👋

I'm not 100% certain, but I think option 1 is probably best. 👍

kentcdodds avatar May 26 '20 23:05 kentcdodds

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

The release is available on:

Your semantic-release bot :package::rocket:

Xunnamius avatar Jan 16 '23 22:01 Xunnamius

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

The release is available on:

Your semantic-release bot :package::rocket:

Xunnamius avatar Jan 18 '23 12:01 Xunnamius