thlorenz / proxyquireify

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
MIT License
152 stars 24 forks source link

Karma + Karma-bro + proxyquireify don't get along #15

Closed xcambar closed 9 years ago

xcambar commented 9 years ago

Hi, I've been struggling for a day with trying to have proxyquireify in Karma with Karma-bro.

In a few words: Proxyquireify adds the authentic require calls so Browserify bundles them, but it seems that the prelude is not changed so the mocked modules are not loaded.

Here's a example repo: https://github.com/xcambar/karma-bro-proxyquireify

Did I do something wrong? What do you think?

/cc @Nikku because I feel like Karma-bro might be involved in this situation.

thlorenz commented 9 years ago

It looks ok to me except I don't see where you register it as a plugin with browserify, i.e. like here

xcambar commented 9 years ago

here, in the Karma conf

thlorenz commented 9 years ago

Ok, so as long as this plugin gets added to browserify and karma doesn't mess with the prelude and/or the require function things should work.

You could trouble shoot this by debugging the tests and/or reading the relevant parts of your bundle.

xcambar commented 9 years ago

https://github.com/thlorenz/proxyquireify/blob/master/index.js#L70-L72 sets the _stubs and cache then resets right after.

https://github.com/thlorenz/proxyquireify/blob/master/index.js#L71 uses the original require. Shouldn't it call proxyquire.proxy (https://github.com/thlorenz/proxyquireify/blob/master/index.js#L81) ?

I'll test it and PR if relevant.

xcambar commented 9 years ago

For greater details, here's my complete bundle (proxyquireify with the aforementioned changes): https://gist.github.com/xcambar/906c17cdf6fb8a8bdbdc

There's obviously something missing, but for the moment, I can't figure out what.

thlorenz commented 9 years ago

I don't have time to look at this right now. Hopefully someone else can help you figure this out in the meantime, maybe @jhiesey?

ellbee commented 9 years ago

I think the problem is caused by the reset method being called on the browserify instance, which creates a new pipeline that doesn't have the proxyquireify replacement prelude spliced in. This changes made in this commit seem to pass your test: https://github.com/ellbee/proxyquireify/commit/fdbb2daf3ec6a9963d499a53b0b789dbb05eb501

daegren commented 9 years ago

I am also getting the same issue when trying to rebundle with proxyquireify using watchify, however @ellbee's fix just seems to hang on my pipeline. I've put up a small test repo to test this. https://github.com/daegren/proxyquireify-test

ellbee commented 9 years ago

Your test repo works for me with the changes from my commit if I move the plugin registration out from the bundle function, so it only registers it once. If I leave the plugin registration inside the plugin function I get a gulp error on rebundle.

daegren commented 9 years ago

Thanks @ellbee! That worked, much appreciated.

thlorenz commented 9 years ago

@ellbee is there any patch we can pull in here? Do you mind providing a PR? I'm happy to pull this in as long as it doesn't break current behavior.

ellbee commented 9 years ago

@daegren Cool, glad that helped.

@thlorenz I've created a pull request https://github.com/thlorenz/proxyquireify/pull/17

Edit: I changed the pull request slightly, so that it now only adds the transform when the plugin is registered and not every time reset is called.

xcambar commented 9 years ago

Awesome !

beckyconning commented 9 years ago

is there any way to use these changes in order to swap between a proxyquire wrapper and proxyquireify when karma-bro browserifies tests?

i'm in a situation where this:

var proxyquire = require('./proxyquire-wrapper')(require);

works in node and this

var proxyquire = require('proxyquireify')(require);

works in the browser but i can't seem to make karma-bro swap the wrapper for proxyquireify for me.

thanks!

beckyconning commented 9 years ago

I use karma-bro with proxyquireify through https://github.com/bendrucker/proxyquire-universal.

You can use it with karma-bro by simply adding this to your karma configuration.

browserify: { debug: true, plugin: ['proxyquire-universal'] }

xcambar commented 9 years ago

Very cool @beckyconning. Thanks a lot!

bendrucker commented 9 years ago

This is the same as #21 and there's a long discussion there. Turned out it was a one line fix, merging in #22.

K4LiN commented 6 years ago

I've got exactly the same issue with the current versions of proxyquire: "proxyquire-universal": "^1.0.8", "proxyquireify": "^3.2.1",

I checked the @xcambar git repo and applying patch or updating the proxyquirefy to current isn't solving the issue.

Ps. sorry for digging up the thread ;)

bendrucker commented 6 years ago

Because this issue is pretty ancient, I have to ask that you open a new one if you think you've found a legitimate bug. This is in large part in fairness to the original contributors to the discussion who shouldn't be forced to unsubscribe from notifications on a 3 year old thread.

It's possible a major version of browserify or karma* broke something. To start we'll need a project (public repo) that we can clone, run npm install && npm test, and get exit 1 with a trace of the error you're seeing. As few deps as possible, e.g. use assert and not something like mocha.