laconiar
v1.4.7
Published
requirejs like service for laconia
Downloads
3
Maintainers
Keywords
Readme
laconiar
requirejs like service for laconia
Why?
Every time you use require
you make it tightly coupled with required library. This isn't good for tests and SOLID principles.
With this you can `require' things and inject/mock them when testing
Installation
npm install --save laconiar
Usage
// app.js
module.exports = (event, {
R: {
fs, // this same as const fs = require('fs');
fs: { createReadStream }, // this same as const { createReadStream } = require('fs');
'aws-sdk': awsSDK, // this is same as const awsSDK = require('aws-sdk');
'./spam': err, // this will not work;
`${__dirname}/spam`: spam // this will work
}
}) => {
// do something with required modules;
return createReadStream();
};
// index.js
const laconia require('@laconia/core');
const R = require('laconiar');
const app = require('./app');
exports.handler = laconia(app).register(R());
// app.test.js
const app = require('./app');
describe('testing app', () => {
it('mocking requirements', () => {
const createReadStream = jest.fn();
app(
R: {
fs: { createReadStream }
}
);
expect(createReadStream).toHaveBeenCalled();
});
});
FAQ
Should you use it?
Good question, the main feature of Laconia is to be a Micro dependency injection framework, some DI frameworks recommends to do not use it for things like require external library.
My point is that you may use it for service creation.
How use it with Tree shaking?
You couldn't, since your dependency are loaded dynamically. Isn't not impossible but not implemented yet.
Related
Contributors
Thanks goes to these people (emoji key):
| hugosenari💻 📖 🚇 | | :---: |
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT © hugosenari