Giter Site home page Giter Site logo

Comments (6)

embpdaniel avatar embpdaniel commented on May 18, 2024 1

Thank you @theKashey . I have now been able to set it up 👍

from rewiremock.

theKashey avatar theKashey commented on May 18, 2024

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):

  1. Use returned mock object somehow.
  2. Recreate your express server, to include the override
  3. 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.

embpdaniel avatar embpdaniel commented on May 18, 2024

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:

  1. mocha command targeting tests folder is ran
  2. tests/index.js runs, uses real server module
  3. server module initializes express and routes, and requires 'firebase-admin' to initialize it
  4. test runs for auth route
  5. rewiremock attempts to mock any request to 'firebase-admin' within it function
  6. test invokes a route to 'controllers/auth', passing token
  7. auth route requires 'firebase-admin' to attempt to verify token
  8. 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.

theKashey avatar theKashey commented on May 18, 2024

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.

embpdaniel avatar embpdaniel commented on May 18, 2024

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.

theKashey avatar theKashey commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.