-
Notifications
You must be signed in to change notification settings - Fork 135
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
Comments
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 }) |
Thanks for the quick reply. Example:
This is a sample module that can use the previous one
The unit test file
|
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:
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? |
Yes it makes sense. The old library required to use the
That's why is used the same with |
Ok I managed to understand better how to refactor the codebase and make it simpler. |
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?
The text was updated successfully, but these errors were encountered: