CLI for creating, editing and modifying an application using inversify dependency injection.
npm install inversify-typescript-cliSpeeds up some of the boilerplate required to build apps with dependency injection.
This project is a work in progress. Contributions and feedback are welcome.
Install inversify-cli
``yarn add inversify-cli`
In the scripts section of your package.json file, assign a script to inversify-cli to create an alias
``
...
"scripts": {
"cli": "inversify-cli"
},
...
> NOTE: replace "cli" in all examples below with whatever string you use as the script name.
Optionally create an inversify-cli.json file in your project root.
inversify-cli.json
``
{
"dir": "./src", // Folder to create the typescript files
"componentTests": true // Enable generation of component tests
}
Initialize your project.
``
yarn cli init
Files will be created in the folder specified by inversify-cli.json.
Inside your project folder you will have the following files and directories:
You can modify this class as you like to help simplify your testing. For example, I suggest adding a helper function that uses testdouble to rebind a reference with a mocked version. This helps isolate units of code.
Add a module with the command
yarn cli module create
A module container will be created and added to the application container.
Add a component to a module with the command
yarn cli component create
A component will be created and added to the module container.
To simplify unit testing you can use the harness to reference any component registered in the application container. Additionally, you can pass in a function that will let you get an instance of the container so that you can rebind any dependencies as you see fit.
Before your tests bootstrap your harness (referred to below as "h").
In your tests you can reference the component you are testing with h.sut (System Under Test).
`
import * as Fs from 'fs-extra';
import * as Path from 'path';
import { H } from './../../../harness';
import { ref } from './../../../ref';
describe('Sample Test', () => {
let h: H
let dblFs: typeof Fs;
let dblPath: typeof Path;
beforeEach(() => h = new H
ref.ClassUnderTest,
(harness) => {
// Rebind some dependencies to a constant value
// to avoid side effects in tests
//dblFs = harness.container.rebind(ref.Fs).toConstant({});
//dblPath = container.rebind(ref.Path).toConstantValue({});
}
));
describe('sampleMethod', () => {
it('should return true', async () => {
expect(h.sut.sampleMethod()).to.eql(true);
});
});
});
``
- Refactor the application to itself use inversify
- Throw more informative errors
- Add unit and e2e tests
- Throw error if attempting to recreate a module or container that already exists