Giter VIP home page Giter VIP logo

Comments (5)

jeffijoe avatar jeffijoe commented on August 28, 2024

Not sure what you mean, could you elaborate, perhaps with an example of what you're trying to do?

If you are talking about unit tests, then you don't need Awilix in your tests because your code now uses plain constructors/factories to accept dependencies.

const service = new MyService({ db: dbMock })

from awilix.

leorossi avatar leorossi commented on August 28, 2024

Thanks for the quick reply.
I am migrating also the test suite which originally was using the old library, so I am not sure the same approach will work with awilix. Here is an example

Example:
file api_utils.js is the module to be tested.

module.exports = function({ env, log  }) {
  const API_Utils = function API_Utils() {

  }
  API_Utils.prototype.doStuff = function() {
    log.info('Doing some stuff');
  }
  return API_Utils;
};

This is a sample module that can use the previous one

module.exports = function({ api_utils }) {
  apiUtils = new api_utils();
  apiUtils.doStuff();
}

The unit test file test/api_utils.js (using mocha)

...
describe('API_Utils', () => {
  it('should log "Doing some stuff"', (done) => {
    const mocks = {
      log: {
        info: function(message) {
          assert.equals(message, 'Doing some stuff');
        }
      }
    }
   // How should I inject mocks.log ?
  });
});

from awilix.

jeffijoe avatar jeffijoe commented on August 28, 2024

First of all, your general usage seems wrong to me, since you are exporting a function which itself creates a constructor function.

This is how I would do it:

api_utils.js

module.exports = API_Utils

function API_Utils ({ env, log  }) {
  this.env = env
  this.log = log
};

API_Utils.prototype.doStuff = function() {
  this.log.info('Doing some stuff');
}

And the usage:

module.exports = function({ apiUtils }) {
  apiUtils.doStuff();
}

And finally the test — your mocks object can be used as-is.

const APIUtils = require('../path/to/api_utils.js')

describe('API_Utils', () => {
  it('should log "Doing some stuff"', (done) => {
    const mocks = {
      log: {
        info: function(message) {
          assert.equals(message, 'Doing some stuff');
        }
      }
    }
    const apiUtils = new APIUtils(mocks)
    // Use here
  });
});

Does this make sense?

from awilix.

leorossi avatar leorossi commented on August 28, 2024

Yes it makes sense.

The old library required to use the

module.exports = function(dep1, dep2 ... ) {
 // here create the returnable object
}

That's why is used the same with awilix. Will test soon and update (or close) the issue. Thanks for now

from awilix.

leorossi avatar leorossi commented on August 28, 2024

Ok I managed to understand better how to refactor the codebase and make it simpler.
I have another question but I'll close this issue and open another, after searching for previous similar issues. Thanks.

from awilix.

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.