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

TypeError: Cannot assign to read only property '__get__' of object '#<Object>' #163

Closed yvele closed 7 years ago

yvele commented 7 years ago

Problem

I get the following error using babel-plugin-rewire@1.0.0 and node@6.3.0

/Users/me/project/lib/MyClass.js:181
exports.__get__ = _get__2("_get__");
                ^

TypeError: Cannot assign to read only property '__get__' of object '#<Object>'

Related to https://github.com/speedskater/babel-plugin-rewire/issues/133 (which is closed)

Source code

MyClass.js

const BAR = "bar";

export default class MyClass {
  foo() {
    return BAR;
  }
}

.babelrc (babel-cli@6.14.0)

{
  "presets": ["latest"],
  "env": {
    "test": {
      "plugins": ["rewire"]
    }
  }
}

Behaviour

Actually, rewire exports are made after default export :

exports.default = MyClass;

....

exports.__get__ = _get__;
exports.__GetDependency__ = _get__;
exports.__Rewire__ = _set__;
exports.__set__ = _set__;
exports.__ResetDependency__ = _reset__;
exports.__RewireAPI__ = _RewireAPI__;

Rewire exports should be before default export :


....

exports.__get__ = _get__;
exports.__GetDependency__ = _get__;
exports.__Rewire__ = _set__;
exports.__set__ = _set__;
exports.__ResetDependency__ = _reset__;
exports.__RewireAPI__ = _RewireAPI__;

exports.default = MyClass;

(or something like that...)

Workaround

Not using default... :confused:

MyClass.js

const BAR = "bar";

export class MyClass {
  foo() {
    return BAR;
  }
}
speedskater commented 7 years ago

@yvele thanks for providing the sample. I have added a sample (https://github.com/speedskater/babel-plugin-rewire/tree/master/samples/issue163) for your issue, but was not able to reproduce your issue. Could you please have a look at the sample and let me know whether it runs on your system?

speedskater commented 7 years ago

@yvele anything new from your side?

yvele commented 7 years ago

@speedskater Sorry I'm not using babel-plugin-rewire anymore in my projects :persevere:

speedskater commented 7 years ago

@yvele sorry to hear that. As i am not able to reproduce your error I will close this issue for now.

ydaniv commented 7 years ago

Getting the same issue on version 1.1.0 and node@7.10.0. Removing default doesn't seem to solve it. Any idea?

emerson-h commented 7 years ago

I can confirm that tis is an issue with 1.0.0 and Node 7.9.0 as well.