Comments (13)
I have no idea how this path 'V:GitHubTestRunnerode_modulesproxyquireproxyquire.js' was munged like that.
I doubt however that proxyquire had any part in it.
What version of nodejs are you using?
If you want to troubleshoot this, I'd suggest setting the temp directory to something obvious (i.e. c:\tmp as explained here and commenting this line in proxyquire inside your node_modules/proxyquire/proxyquire.js
.
You should then have a mocha.js@x
file in your temp directory that proxyquire generated. You can compare that to the original to figure out if proxyquire caused problems.
BTW, I hope you are not actually mocking out the mocha test runner that is running your tests? I don't understand how that would be useful.
from proxyquire.
Thanks, i'll try and debug some more. The code works fine on Linux and OSx.
And lol, I am mocking the test runner but not while it runs my tests. The module I am writing (actually I wrote a while back but wanted to get under test) is a plugin for grunt that runs tests using mocha. So in the tests for my plugin I have mocked, grunt, mocha and the module cache so they don't interfere with the currently running tests (which are also using mocha and in fact the plugin under test)... um, not at all confusing, but hey, like i say, it's working everywhere but windows.
from proxyquire.
Ok, let me know if you find a solution or need more help.
from proxyquire.
Ah ha, I can see the problem. It could be the resolution of __dirname on windows:
var __dirname = "V:\GitHub\grunt-mocha-test\tasks"; var __filename = "V:\GitHub\grunt-mocha-test\tasks\mocha.js"; function require(mdl) { return module.require("V:\GitHub\grunt-mocha-test\node_modules\proxyquire\proxyquire.js")._require(mdl, "C:\Users\PHALLI~1\AppData\Local\Temp\mocha.js@0", __dirname); }
as you can see this contains single backslashes, hence the munging due to escaping (wondered where the newline came from). I can probablty fix this externally for __dirname but i can see that there may be a problem with the temp file location too (at the end of that code snippet). So it may also be necessary to override the temp directory on windows. I will continue to investigate
from proxyquire.
Ok i think the best solution is to escape the backslashes within proxyquire. I'm currently testing this locally and will likely make a pull request shortly
from proxyquire.
Looks like you are on the right track.
Something along the lines of:
'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
[..]
should work.
from proxyquire.
Yeah I have this currently but can't figure out the best way to get it under test
[ 'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
, 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '
, 'function require(mdl) { '
, 'return module'
, '.require("' , __filename.replace(/\\/g, '\\\\'), '")'
, '._require(mdl, "' + resolvedProxy.replace(/\\/g, '\\\\') + '", __dirname); '
, '} '
as it was though only this test fails which I find odd (I thought more would)
describe('Multiple requires of same module don\'t affect each other', function () {
describe('Given I require foo stubbed with bar1 as foo1 and foo stubbed with bar2 as foo2', function () {
anyway with my change it now passes but this can only be verified on windows (ie. Travis won't tell you if it breaks will it?)
from proxyquire.
It won't fail on travis if it is only windows related.
Some suggestions:
Ideally you'd put all that duplicate code into function.
You don't need __filename.replace(/\\/g, '\\\\')
since you fixed __filename
above.
Make sure that all tests pass and put a comment on the test that only will fail on windows if you remove your fixes (I'm assuming you wrote a test to expose the problem first).
from proxyquire.
Yup i think you're right about the duplicate code - i'll change that.
Actually i didn't write a new test in the end as the test i mentioned already failed on windows. I'll see if i can split it into a separate test though that only checks this issue and comment that one as only failing on windows.
I was surprised that only that test failed though as all the tests use the resolve function at some point.
Not sure i understand why you think this is not needed:
__filename.replace(/\\/g, '\\\\')
It's not really fixed above if you mean here:
, 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '
as that bit of code doesn't get evaluated till later and is not the same __filename variable
from proxyquire.
Unless I'm missing something, __filename
should have been escaped since resolvedFile was. No need to escape it again (it's the same variable).
Same reason why you don't have to escape __dirname
again, which I see you didn't.
Try without it and see what you get in the generated file.
Also if the problem was exposed with an existing test and now passes, that is fine. If you want to add tests for other potential problems that are now fixed, please do so.
from proxyquire.
i think it's clearer if i add code colouring
[ 'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
, 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '
, 'function require(mdl) { '
, 'return module'
, '.require("' , __filename.replace(/\\/g, '\\\\'), '")'
, '._require(mdl, "' + resolvedProxy.replace(/\\/g, '\\\\') + '", __dirname); '
, '} '
I have to escape the stuff before it gets added to the literal - the stuff in the literal doesn't need to be escaped.
Anyway working on some tests that will prove each change independently (I hope)
from proxyquire.
ok see it now. thanks.
go ahead and make a pull request once the duplicate code is cleaned up, I'll merge it in tonight and publish a new version.
Thanks for contributing!
from proxyquire.
Your welcome :) Cleaning up now
from proxyquire.
Related Issues (20)
- Problem when stubbing for just one test HOT 1
- Unable to stub indirect improts HOT 3
- TypeScript definitions HOT 1
- Using `@global` on path with subdir causes Node to throw MODULE_NOT_FOUND error HOT 4
- NodeJS karma proxyquire gives Error: ENOENT: no such file or directory, scandir C:\\<path to project>\\[object Object][object Object] HOT 1
- Examples of use with classes HOT 2
- Cannot proxy module sub-item HOT 3
- Mocks the first require only HOT 1
- Unable to resolve module `module` from `node_modules/proxyquire/lib/proxyquire.js` HOT 1
- Slightly confusing first example in readme HOT 1
- `createRequire` support & a way to access the mocked `require` HOT 6
- Stubbing a dependency within a base class HOT 2
- proxyquire jasmine expect not working - 1 test 0 assertions HOT 2
- How to use multiple ways to override the imported function in controller using proxyquire HOT 1
- Stubbing peer dependencies HOT 3
- Can we stub pg library of nodejs using proxyquire? HOT 1
- Proxyquire compatibility with ESM HOT 1
- Proxyquire not working with node@16? HOT 1
- Cannot read property 'require' of undefined when using ES6 modules HOT 5
- module exporting a function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proxyquire.