Comments (6)
Thank you @theKashey . I have now been able to set it up 👍
from rewiremock.
A few questions:
- what is
request
- how
controllers/auth
got called - when do you setup your application?
What rewiremock.proxy
does:
- requires module with overridden dependencies
- returns it
- clears everything
Override does not exists before and after rewiremock.proxy
.
What you probably shall do(these are all different variants):
- Use returned
mock
object somehow. - Recreate your express server, to include the override
- Dont disable rewiremock.
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()
})
from rewiremock.
hi @theKashey thanks for your suggestions. I tried option 3, but I get this error:
TypeError: rewiremock.mock is not a function
Maybe it has something to do with the way I required rewiremock
? (const rewiremock = require('rewiremock/node').default
)
To answer your other questions:
What is
request
Request comes from supertest
, basically all I do is const request = require('supertest')
at the top, then I pass app
which is basically, the top-level Mocha index.js file. That file requires my 'server' file (not currently mocked), and the server file requires 'firebase-admin' to initialize firebase. Just making a note of that in case it matters.
How
controllers/auth
got called
The server
module I initialized in the Mocha index.js is what connects the express server and the routes. When I use supertest
to route (request(app).post(/some/endpoint/)
), it will find the appropriate route. So there is no need for me to import the actual module into my tests as it is invoked via supertest
.
when do you setup your application?
To explain a bit more on the order:
- mocha command targeting tests folder is ran
- tests/index.js runs, uses real server module
- server module initializes express and routes, and requires 'firebase-admin' to initialize it
- test runs for auth route
- rewiremock attempts to mock any request to 'firebase-admin' within
it
function - test invokes a route to 'controllers/auth', passing token
- auth route requires 'firebase-admin' to attempt to verify token
- real 'firebase-admin' library is used, instead of the mock
Hope this helps, let me know if any more info, thanks again!
Danny
from rewiremock.
rewiremock.mock is not a function
// just
rewiremock('firebase-admin').with(new function (location) { return {} });
Regarding the rest - after you have created server
- you can't change it. It's already created.
You have to recreate server with some parts mocked(by the time of creation), and that's the only way to do the job. That's not how mocking works, but how javascript works.
from rewiremock.
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?
from rewiremock.
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)
from rewiremock.
Related Issues (20)
- Documentation Needed: jest.mock Equivalent HOT 1
- Please add a test for a module that returns a Class that gets newed up by the SUT HOT 1
- Something like cache problem HOT 3
- Testing a private function using rewiremock HOT 2
- Mocking non existent modules HOT 4
- how to use in webpack5
- Angular 11
- Usage inquiry ES6 HOT 1
- webpack.NamedModulesPlugin() has been deprecated HOT 12
- Configuration for Mocha/webpack5 HOT 1
- nodejs v18.3.0 Error: Rewiremock: there is no "parent module". Is there two HotModuleReplacementPlugins? HOT 3
- Typescript setup Error: Rewiremock: there is no "parent module". Is there two HotModuleReplacementPlugins?
- Mock called, but not stubbing dependency HOT 2
- Invoking 'proxy' to generate test object but stub not invoked HOT 3
- Is it enable can execute like a jest.mock? HOT 3
- Setup for multiple test files HOT 4
- Does rewiremock work with ts-node? HOT 3
- Question: Mocking local variable in the scope of original module HOT 1
- Ts-node and rewiremock HOT 2
- Mocking external module import HOT 2
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 rewiremock.