bendrucker / proxyquire-universal

Proxyquire in Node and Proxyquireify in the browser with no code changes
MIT License
35 stars 5 forks source link

Issue when used with karma-browserify #7

Closed jamestalmage closed 9 years ago

jamestalmage commented 9 years ago

Repo demonstrating the problem here (see the Readme): https://github.com/jamestalmage/karma-browserify-proxyquire-issue

Possibly related to these issues:

When it fails it seems to run twice; Once correctly, then with the following error:

Chrome 40.0.2214 (Mac OS X 10.9.5) ERROR
  Uncaught ReferenceError: require is not defined
  at /Users/jamestalmage/WebstormProjects/karma-browserify-proxyquire-issue/test/add-test.js:1

I do see it occasionally run twice per save, even on the no-proxyquire branch, so there may be underlying issues further up the tool chain. I've got a moderately larger project (5 source files, 3 test files, 44 tests), that also uses browserify-shim to require angular. The problem reproduces with far more regularity in that project. I originally tried a test project using only browserify-shim to require angular, and could not reproduce the error.

Console dump:

INFO [watcher]: Changed file "/Users/jamestalmage/WebstormProjects/karma-browserify-proxyquire-issue/test/sum-test.js".
DEBUG [framework.browserify]: updating test/sum-test.js in bundle
DEBUG [framework.browserify]: resetting bundle
DEBUG [framework.browserify]: bundling
INFO [framework.browserify]: 1009849 bytes written (0.13 seconds)
INFO [framework.browserify]: bundle updated
DEBUG [karma]: List of files has changed, trying to execute
DEBUG [karma]: All browsers are ready, executing
DEBUG [framework.browserify]: files changed
INFO [watcher]: Changed file "/var/folders/2_/zg9h6_xd4r3_z7c07s0cn8mw0000gn/T/a083555b338505dbab382401272ac601.browserify".
DEBUG [web-server]: serving (cached): /Users/jamestalmage/WebstormProjects/karma-browserify-proxyquire-issue/node_modules/karma/static/context.html

  add
    ✓ adds things

  sum - proxyquire
    ✓ sums things

Chrome 40.0.2214 (Mac OS X 10.9.5): Executed 2 of 2 SUCCESS (0.008 secs / 0.002 secs)

DEBUG [framework.browserify]: resetting bundle
DEBUG [framework.browserify]: bundling
INFO [framework.browserify]: 1009841 bytes written (0.14 seconds)
INFO [framework.browserify]: bundle updated
INFO [watcher]: Changed file "/var/folders/2_/zg9h6_xd4r3_z7c07s0cn8mw0000gn/T/a083555b338505dbab382401272ac601.browserify".
DEBUG [karma]: List of files has changed, trying to execute
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving (cached): /Users/jamestalmage/WebstormProjects/karma-browserify-proxyquire-issue/node_modules/karma/static/context.html
Chrome 40.0.2214 (Mac OS X 10.9.5) ERROR
  Uncaught ReferenceError: require is not defined
  at /Users/jamestalmage/WebstormProjects/karma-browserify-proxyquire-issue/test/add-test.js:1

Chrome 40.0.2214 (Mac OS X 10.9.5): Executed 0 of 0 ERROR (0.293 secs / 0 secs)
bendrucker commented 9 years ago

Given the range of setups where people have seen this I'm wondering whether it's related to any of these transforms at all. I'm inclined to think it might just be a race condition that only comes up for expensive transforms (esp. ones that parse and walk the AST). That would apply to both proxyquireify (which this issue should also exist with when used directly) and b-shim for sure. Digging in to your reproduction now. Thanks for the clear report as always.

bendrucker commented 9 years ago

I'm actually able to get an error right away if I save any file from ./src, i.e. something added to the bundle by a require instead of by karma-browserify. This looks suspiciously like https://github.com/thlorenz/proxyquireify/issues/21.

jamestalmage commented 9 years ago

Yup, thought you fixed that ;)

See latest commits. Also broken in proxyquireify.

bendrucker commented 9 years ago

That would make sense. proxyquireify-universal is swapping in proxyquireify before any of this stuff should be happening. We can just keep this issue open.

bendrucker commented 9 years ago

I've tracked it down. It's closely related to thlorenz/proxyquireify#21. Fix is coming.

bendrucker commented 9 years ago

Because proxyquire-universal uses peerDeps, you should always be running npm i -D proxyquire-universal proxyquireify proxyquire. So just switch up proxyquireify to thlorenz/proxyquireify#5762d478a22bb76807e3b92a3b881c42686b3b69 temporarily and you should be good.

jamestalmage commented 9 years ago

awesome. I'm still seeing an occasional weird double run - but no test failures.

you should always be running npm i -D proxyquire-universal proxyquireify proxyquire

yup, repo was built in haste

bendrucker commented 9 years ago

Added a comment to the readme about installing. Right now peerDeps auto installed but somewhere an npm employee mentioned that peerDeps are eventually going to just be warnings.

bendrucker commented 9 years ago

The double run is on karma-browserify's end. I've never been able to properly track that down.

chinedufn commented 9 years ago

I still get this 'require is undefined' issue when I install deps via:

// Did not work
npm i -D proxyquire-universal proxyquireify proxyquire

However... This worked:

// Did work
npm i -D proxyquire-universal thlorenz/proxyquireify#5762d478a22bb76807e3b92a3b881c42686b3b69 proxyquire

Forgive my lack of npm-fu here... But I'm assuming we're just waiting on @bendrucker's proxyquireify fix to be published to npm before the 1st snippet will work?

jamestalmage commented 9 years ago

correct

bendrucker commented 9 years ago

Just pinged Thorsten to release p-ify

https://github.com/thlorenz/proxyquireify/commit/5762d478a22bb76807e3b92a3b881c42686b3b69#commitcomment-9896436

bendrucker commented 9 years ago

published