-
Notifications
You must be signed in to change notification settings - Fork 31
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
Mocking with supertest in Node, not working? #68
Comments
A few questions:
What
Override does not exists before and after What you probably shall do(these are all different variants):
So simplest way is 3 rewiremock('firebase-admin').with(new function (location) { return {} });
rewiremock.enable();
// rewiremock is active and will override any require to `firebase-admin`
// just do that require (probably re-create server)
request(app)
.post(API_URL)
.send({ phone: PHONE, token })
.end((err, res) => {
// disable it
rewiremock.disable();
if (err) return done(err)
expect(res.statusCode).to.be.equal(200)
done()
}) |
hi @theKashey thanks for your suggestions. I tried option 3, but I get this error: Maybe it has something to do with the way I required To answer your other questions:
Request comes from
The
To explain a bit more on the order:
Hope this helps, let me know if any more info, thanks again! |
// just
rewiremock('firebase-admin').with(new function (location) { return {} }); Regarding the rest - after you have created |
Oh, I see. So my problem is, I am trying to mock after I have already required 'firebase-admin' by my server module. So I need to mock 'firebase-admin' before requiring the server. And then to have the most control possible, ideally I would need to create a mocked server before each test runs, so that I have full control of my environment in the test. Am I understanding the flow of things? |
Yep. You shall first setup your environment, next create subject under test, then test it. Things like Jest, which runs tests in isolation might help here, especially if you will use jest-like mocking (https://github.com/theKashey/rewiremock#hoisted-mocking) |
Thank you @theKashey . I have now been able to set it up 👍 |
Hey there, brand new to your library. I am using supertest to test my Express routes, and one of my route files uses the firebase admin sdk.
I want to mock the firebase admin sdk, so as a first test to confirm that
rewiremock
is working, I have assigned an empty object as the dependency for firebase. I proxy the module that handles my routes '../api/controlelrs/auth', and this is the module that directly imports the 'firebase-admin' module from the firebase sdk:What happens here is that
verifyIdtoken
from the REAL firebase admin sdk is called within my route. I verified by adding a console.log inside of the real firebase-admin library.What I expected to happen was for my test console.log to not be called, as I'm hoping that the
rewiremock
library will replace the real firebase SDK with a mock (in the case of this example, an empty object).Like I said, I'm new to using your library, maybe I am not setting up something correctly. Can you help? Thank you!
Danny
The text was updated successfully, but these errors were encountered: