Skip to content
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

Inject mocks for test suite #37

Closed
leorossi opened this issue Sep 26, 2017 · 5 comments
Closed

Inject mocks for test suite #37

leorossi opened this issue Sep 26, 2017 · 5 comments
Labels

Comments

@leorossi
Copy link

I migrated from another DI library to awilix. I migrated the codebase but now I am struggling with test suite, since I use mocks for many of the tests I am wondering how I can inject my custom modules after I register them in the container.
Is there a way to do that?

@jeffijoe
Copy link
Owner

jeffijoe commented Sep 26, 2017

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

@leorossi
Copy link
Author

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 ?
  });
});

@jeffijoe
Copy link
Owner

jeffijoe commented Sep 26, 2017

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?

@leorossi
Copy link
Author

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

@leorossi
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants