Comments (5)
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.
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.
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.
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.
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)
- Whiteboard pattern HOT 7
- [Question] Support for assisted inject HOT 2
- awilix object is undefined when using default import, but named import of functions work HOT 1
- Support for ESM modules HOT 3
- Is it possible to reinforce constructor param name/types? HOT 1
- Can't resolve HOT 4
- How to unregister a dependency from the container? HOT 6
- Default value not working in class constructor in Proxy mode HOT 2
- asFunction -> asClass Cyclic dependency HOT 2
- Awilix TypeError: Cannot read properties of undefined (reading 'cache') HOT 2
- Calling `loadModules` does not register all the file paths HOT 2
- Is it possible to detect / error on singletons dependent on transient or scoped registrations? HOT 1
- Awilix 10 type exposure HOT 1
- [QUESTION] Userland async initializers HOT 7
- replace camel-case dependency HOT 3
- Possible valid use-case for disabling strict mode for specific resolver HOT 2
- Critical dependency: the request of a dependency is an expression HOT 3
- Add TypeScript support HOT 1
- [QUESTION] - Awilix Fails to Autoload Modules in Vitest Tests with TypeScript Setup HOT 1
- How can I get scope inside instance? HOT 14
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 awilix.