-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question - dealing with require.exports = ClassConstructor #144
Comments
What are you expecting proxyquire to do here for you? Automatically set up a constructor and clone a prototype for you? Because it doesn't do that. |
Is there a different approach I could be taking? In particular I don't like the fact that I have to explicitly |
If you pass in a function as a stub proxyquire will try to extend your stub's prototype with the original: https://github.com/bendrucker/fill-keys/blob/master/index.js#L10-L12 So if you want to override a class, you have to override the constructor (always) but can provide only the method you intend to override on your stub's prototype. Does that help? |
Hmm, I think that's the answer I was looking for. Thanks! Thanks for the quick replies. |
No problem. I agree that class examples would be helpful. Will keep it in mind for future work. |
Hey @d1manson did you ever figure out how to optimize this with proxyquire? Grappling with the same issue now. |
Hey Ben, thanks for all your great work on proxyquire! Just one question if I may. I'm asking proxyquire to stub some methods in a mongoose model. When the model gets loaded in the underlying module (Node), proxyquire is turning it into an object, and removing it's javascript class nature (constructor). This means I can no longer call "new" on the model object to create a new instance of the model. I was hoping it would just load the Model class/constructor in like normal, and then replace the methods I give it to replace. By rendering the Model class as a javascript object, it basically breaks the code. Can anything be done? |
Use |
I originally asked this on SO but got no reply - I'm guessing here would be a better bet....
If I have a module that exports a single class, how can I mock/stub it with
proxyquire
?Currently I have the following, which seems to work, but is rather lengthy and doesn't make proper use of
proxyquire
:some-class.js
my-module.js
test-my-module.js
Note that I am fairly new to mocking (and testing!) so I might be missing something obvious, but I was hoping that
proxyquire
would deal with my problem itself (rather than me needing to use another library or write the above code).Thanks!
The text was updated successfully, but these errors were encountered: