speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

babel-plugin-rewire breaks babel-preset-power-assert #180

Open sazzer opened 7 years ago

sazzer commented 7 years ago

I've no idea if this is a problem in babel-plugin-rewire or babel-preset-power-assert, but if I have both in my build at the same time then babel-plugin-rewire works and babel-preset-power-assert doesn't do anything.

With my Babel configuration looking like:

{
  "sourceMap": true,
  "presets": [
    "latest",
    "power-assert"
  ],
  "plugins": [
    "transform-runtime"
  ]
}

Then the following transformation happens:

    assert(adder(a, b) === 5);

into

    (0, _powerAssert2.default)(_rec._expr(_rec._capt(_rec._capt(adder(_rec._capt(a, 'arguments/0/left/arguments/0'), _rec._capt(b, 'arguments/0/left/arguments/1')), 'arguments/0/left') === 5, 'arguments/0'), {
        content: 'assert(adder(a, b) === 5)',
        filepath: 'src/server/demo.test.js',
        line: 12
    }));

But when I add babel-plugin-rewire in to the build, as such:

{
  "sourceMap": true,
  "presets": [
    "latest",
    "power-assert"
  ],
  "plugins": [
    "rewire",
    "transform-runtime"
  ]
}

Then the exact same line is transformed into

    _get__('assert')(adder(a, b) === 5);
sazzer commented 7 years ago

As a workaround, if I user the individual plugins instead of the preset I can make it work. I have to have a configuration of:

{
  "sourceMap": true,
  "presets": [
    "latest"
  ],
  "plugins": [
    "babel-plugin-empower-assert",
    "babel-plugin-espower",
    "rewire",
    "transform-runtime"
  ]
}

And now that same line is converted into:

    _get__('assert')(_rec._expr(_rec._capt(_rec._capt(adder(_rec._capt(a, 'arguments/0/left/arguments/0'), _rec._capt(b, 'arguments/0/left/arguments/1')), 'arguments/0/left') === 5, 'arguments/0'), {
        content: 'assert(adder(a, b) === 5)',
        filepath: 'src/server/demo.test.js',
        line: 12
    }));

I assume that what is happening is babel-plugin-rewire is being applied before the babel-preset-power-assert, and so the plugins from the preset aren't finding the code they expect - because it's already been re-written - and so they are failing to do the conversion.

speedskater commented 7 years ago

@sazzer Thanks for your report and sorry for the late reply. I think the problem is not very easy to resolve, as it might seem to be a an ordering issue.

babel plugin rewire replaces assert with get('assert') which itself returns the assert variable with a function returning the assert variable. I think this destroys your logic to detect, whether to use the power assert conversion.

The workaround with explicitly specifying the execution order is therefore a viable solution. An alternative approach is to specifiy assert in the list of ignoredIdentifiers. This should prevent assert from being rewired at all.

An alternative approach would be to trace the assert variable, but imho I think this is impossible.