i-like-robots / rewireify

Rewireify is a port of Rewire for Browserify that adds setter and getter methods to each module so that their behaviour can be modified for better unit testing.
https://www.npmjs.com/package/rewireify
Other
59 stars 10 forks source link

Intermittent error attempting to define __get__ more than once on Karma startup. #3

Closed iamrandys closed 9 years ago

iamrandys commented 10 years ago

Error: Invariant Violation: ReactCompositeComponentInterface: You are attempting to define __get__ on your component more than once. This conflict may be due to a mixin.

First off, I do not believe this is a rewireify issue (rewirify has been solid), but from the error message this seems the most logical place for others to look for this issue.

About 1 out of 10 times and only on initial Karma startup, I get the following error and Karma hangs. It is very hard to troubleshoot the issue since it does not happen all the time. I am using:

"karma": "^0.12.21",
"karma-bro": "^0.6.2",
"karma-jasmine": "^0.2.2",
"react": "~0.11.0",
"reactify": "^0.14.0",
"rewireify": "~0.0.9",
i-like-robots commented 10 years ago

Could you post a little more code, maybe one of your React component modules?

iamrandys commented 10 years ago

I'll create a simplified project that exposes the issue. I'll probably figure it out once I do this.

On Sep 1, 2014, at 5:20 AM, Matt Hinchliffe notifications@github.com wrote:

Could you post a little more code, maybe one of your React components?

— Reply to this email directly or view it on GitHub.

i-like-robots commented 10 years ago

That would be great if you could, nobody wants an unsolved mystery!

iamrandys commented 10 years ago

Ok, I've figured it out. After I upgraded to karma-bro 0.7.0, the issue was consistent so I was able to track it down. If you have a react component with mixins defined, the get and set are defined both on the component and the mixin. When React extends the component with the mixin, there are two get methods and it throws an invariant violation. So the issue isn't with any package, it's just a side effect of the way React behaves in a test environment when using mixins. Now I just have to come up with a way to disable mixins when doing unit tests.

iamrandys commented 10 years ago

I was able to move the mixins to another module and then clear the array before requiring the component that used the mixins so that there would not be any mixins included during unit tests.

mixinsModule.get('mixins').length = 0;

i-like-robots commented 10 years ago

Thanks @iamrandys, I appreciate the extra time and effort finding out the issue. It did sound like a symptom of something hiding within the complexity of the setup. Hopefully this will be a useful reference for somebody.

adamdonahue commented 9 years ago

But how is this a fix? Wouldn't clearing the mixins break the component?

joneves commented 9 years ago

The problem does not seem to be solved, rewireify is unusable in its current state as it tried to apply getters and setters mutliple times to mixins. The solution mentioned above does not work as the error occurs when processing require constructs during initialisation.

i-like-robots commented 9 years ago

Could you supply some example code?

Always happy to accept a PR too.

joneves commented 9 years ago

I'll see what i can do, is there a way of stubbing out deeply nested components

i-like-robots commented 9 years ago

It wouldn't be too hard to add a filter, in terms of actually understanding the dependency tree I don't know.

iamrandys commented 9 years ago

Since my last post we have been ignoring mixins using the rewireify --ignore option in karma. It would be nice to be able to supply a "*-mixin" wildcard to the ignore option though.

I don't see that this is an issue with rewireify. It does what it is suppose to do. It just doesn't play well when testing React components that use mixins.

joneves commented 9 years ago

Ok. Is it possible to set rewire a child component inside a component.

A has child B which has a private var i want to override. On 1 Apr 2015 02:20, "iamrandys" notifications@github.com wrote:

Since my last post we have been ignoring mixins using the rewireify --ignore option in karma. It would be nice to be able to supply a "*-mixin" wildcard to the ignore option though.

I don't see that this is an issue with rewireify. It does what it is suppose to do. It just doesn't play well when testing React components that use mixins.

— Reply to this email directly or view it on GitHub https://github.com/i-like-robots/rewireify/issues/3#issuecomment-88302781 .

i-like-robots commented 9 years ago

I've had a think about this, it would be nice if the --ignore option could take an array of glob patterns instead of specific paths.

iamrandys commented 9 years ago

I agree this would be really nice. I could include *mixin and I would have to keep updating this setting.

On Apr 16, 2015, at 5:17 AM, Matt Hinchliffe notifications@github.com wrote:

I've had a think about this, it would be nice if the --ignore option could take an array of glob patterns instead of specific paths.

— Reply to this email directly or view it on GitHub.

i-like-robots commented 9 years ago

@iamrandys If you'd like you can now pull down https://github.com/i-like-robots/rewireify/pull/9 and see if it meets your needs.

iamrandys commented 9 years ago

Oh sweet! I'll definitely will test it out and let you know how it works out. Thanks for the help!

On May 14, 2015, at 5:58 AM, Matt Hinchliffe notifications@github.com wrote:

@iamrandys If you'd like you can now pull down #9 and see if it meets your needs.

— Reply to this email directly or view it on GitHub.

i-like-robots commented 9 years ago

Modules can now be ignored via pattern in 69a131a9a9aab1eec4bfc540e7b9da8d4b2dd4c6