diff --git a/src/Module.ts b/src/Module.ts index 60c9853..5742899 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -124,7 +124,7 @@ export class Module { * Bootstrap the module * @return The module injector */ - bootstrap(): Injector { + async bootstrap(): Promise { const modules: Module[] = [] const $injectors: Injector[] = [] @@ -134,14 +134,21 @@ export class Module { $injectors.push($injector) // TODO maybe throw if a service instance try to be injected in the config hook - modules.forEach((module, i) => { - module.configHooks.forEach(hook => $injectors[i].invoke(hook)) - }) - - modules.forEach((module, i) => { - module.runHooks.forEach(hook => $injectors[i].invoke(hook)) - }) + for (let i = 0; modules.length > i; i++) { + let module = modules[i] + for (let j = 0; module.configHooks.length > j; j++) { + let hook = module.configHooks[j] + await $injectors[i].invoke(hook) + } + } + for (let i = 0; modules.length > i; i++) { + let module = modules[i] + for (let j = 0; module.runHooks.length > j; j++) { + let hook = module.runHooks[j] + await $injectors[i].invoke(hook) + } + } return $injector } diff --git a/test/injector.test.ts b/test/injector.test.ts index 147bf10..eff712e 100644 --- a/test/injector.test.ts +++ b/test/injector.test.ts @@ -5,8 +5,8 @@ import { radis, Injector } from '../src' /* tslint:disable:no-empty */ describe('Injector', function() { - it('the injector should be injected at runtine', () => { - radis + it('the injector should be injected at runtine', async () => { + await radis .module('module', []) .run($injector => { expect($injector).toBeInstanceOf(Injector) @@ -14,8 +14,8 @@ describe('Injector', function() { .bootstrap() }) - it('the injector should be injected at config time', () => { - radis + it('the injector should be injected at config time', async () => { + await radis .module('module', []) .config($injector => { expect($injector).toBeInstanceOf(Injector) @@ -23,8 +23,8 @@ describe('Injector', function() { .bootstrap() }) - it('The injector should parse all kind of function declaration', () => { - radis + it('The injector should parse all kind of function declaration', async () => { + await radis .module('module', []) .factory('s1', () => 's1') .factory('s2', () => 's2') @@ -52,23 +52,23 @@ describe('Injector', function() { .bootstrap() }) - it('Should throw on invalid function', () => { + it('Should throw on invalid function', async () => { const test: any = function(a) {} const _toString = Function.prototype.toString Function.prototype.toString = () => 'a =\\> =\\> function (){}' - expect(() => { + await expect( radis .module('module', []) .run(test) .bootstrap() - }).toThrow() + ).rejects.toThrow(Error) Function.prototype.toString = _toString }) - it('Should work with no parentesis arrow function', () => { + it('Should work with no parentesis arrow function', async () => { const test: any = function(s1) {} const _toString = Function.prototype.toString - const $injector = radis + const $injector = await radis .module('module', []) .factory('s1', () => 's1') .run(s1 => {}) @@ -90,8 +90,8 @@ describe('Injector', function() { .factory('s2', () => 's2') }) - it('Should return the right service', () => { - module + it('Should return the right service', async () => { + await module .run($injector => { expect($injector.getService('s1')).toBe('s1') expect($injector.getService('s2')).toBe('s2') @@ -99,23 +99,23 @@ describe('Injector', function() { .bootstrap() }) - it('Should throw if service does not exist', () => { - module + it('Should throw if service does not exist', async () => { + await module .run($injector => { expect(() => $injector.getService('s3')).toThrow(Error) }) .bootstrap() }) - it('Should get provider', () => { - module + it('Should get provider', async () => { + await module .run($injector => { expect($injector.getService('s1Provider')).toBeTruthy() }) .bootstrap() }) - it('Should throw when provider do not exist', () => { - module + it('Should throw when provider do not exist', async () => { + await module .run($injector => { expect(() => $injector.getService('s3Provider')).toThrow() }) @@ -142,8 +142,8 @@ describe('Injector', function() { } } - beforeEach(() => { - $injector = radis + beforeEach(async () => { + $injector = await radis .module('module', []) .factory('s1', () => 10) .factory('s2', () => 20) @@ -207,7 +207,7 @@ describe('Injector', function() { }) describe('#instantiate', () => { - it('Should inject services', () => { + it('Should inject services', async () => { class Service { static get $inject() { return ['s1'] @@ -217,11 +217,12 @@ describe('Injector', function() { } } - radis + const $injector = await radis .module('module') .factory('s1', () => 's1') .bootstrap() - .instantiate(Service) + + $injector.instantiate(Service) }) }) @@ -241,8 +242,8 @@ describe('Injector', function() { } } - beforeEach(() => { - $injector = radis + beforeEach(async () => { + $injector = await radis .module('module', []) .factory('s1', () => 's1') .factory('s2', () => 's2') diff --git a/test/module-dependencies.test.ts b/test/module-dependencies.test.ts index 7cf2cc6..e316b09 100644 --- a/test/module-dependencies.test.ts +++ b/test/module-dependencies.test.ts @@ -2,7 +2,7 @@ import radis from '../src' describe('ModuleDependencies', () => { describe('Simple dependency', () => { - it('should get the dependency service', () => { + it('should get the dependency service', async () => { const m1 = radis.module('m1', []).factory('s1', () => 's1') const m2 = radis.module('m2', [m1]).factory('s2', () => 's2') const m3 = radis.module('m3', [m2]).factory('s3', () => 's3') @@ -18,10 +18,10 @@ describe('ModuleDependencies', () => { expect(s3).toBe('s3') }) - m3.bootstrap() + await m3.bootstrap() }) - it('should override the child dependency', () => { + it('should override the child dependency', async () => { const m1 = radis.module('m1', []).factory('service', () => 's1') const m2 = radis.module('m2', [m1]).factory('service', () => 's2') const m3 = radis.module('m3', [m2, m1]).factory('service', () => 's3') @@ -30,19 +30,19 @@ describe('ModuleDependencies', () => { m2.run(service => expect(service).toBe('s2')) m3.run(service => expect(service).toBe('s3')) - m3.bootstrap() + await m3.bootstrap() }) - it('should get the closest child dependency', () => { + it('should get the closest child dependency', async () => { const m1 = radis.module('m1', []).factory('service', () => 's1') const m2 = radis.module('m2', [m1]).factory('service', () => 's2') const m3 = radis.module('m3', [m2]) m3.run(service => expect(service).toBe('s2')) - m3.bootstrap() + await m3.bootstrap() }) - it('should call dependency first', () => { + it('should call dependency first', async () => { const m1 = radis.module('m1', []).factory('s1', () => 's1') const m2 = radis.module('m2', [m1]).factory('s2', () => 's2') @@ -54,10 +54,10 @@ describe('ModuleDependencies', () => { m2.config(s2Provider => expect(count++).toBe(1)) m2.run(s2 => expect(count++).toBe(3)) - m2.bootstrap() + await m2.bootstrap() }) - it('should get a shared instance', () => { + it('should get a shared instance', async () => { const m1 = radis.module('m1').factory('s', () => ({ v: 0 })) const m2 = radis.module('m2', [m1]) const m3 = radis.module('m2', [m1]) @@ -68,10 +68,10 @@ describe('ModuleDependencies', () => { m3.run(s => expect(s.v++).toBe(2)) m4.run(s => expect(s.v++).toBe(3)) - m4.bootstrap() + await m4.bootstrap() }) - it('should be call in the right order', () => { + it('should be call in the right order', async () => { let count = 0 const m1 = radis.module('m1').factory('s', () => { @@ -92,7 +92,7 @@ describe('ModuleDependencies', () => { m3.run(s => expect(count++).toBe(7)) m4.run(s => expect(count++).toBe(8)) - m4.bootstrap() + await m4.bootstrap() }) }) }) diff --git a/test/module.test.ts b/test/module.test.ts index 108efc2..8c0295f 100644 --- a/test/module.test.ts +++ b/test/module.test.ts @@ -4,15 +4,15 @@ import radis from '../src' describe('Module', () => { describe('#bootstrap()', () => { - it('should be called', () => { + it('should be called', async () => { let module = radis.module('module', []) - module.bootstrap() + await module.bootstrap() }) }) // describe('#service()', function() { - it('should be injected', function() { + it('should be injected', async function() { class Service1 {} class Service2 {} class Service3 {} @@ -27,9 +27,9 @@ describe('Module', () => { expect(s2).toBeInstanceOf(Service2) expect(s3).toBeInstanceOf(Service3) }) - module.bootstrap() + await module.bootstrap() }) - it('should be injected with $name', function() { + it('should be injected with $name', async function() { class Service1 { static get $inject() { return ['$name'] @@ -37,7 +37,7 @@ describe('Module', () => { constructor(public $name: string) {} } - radis + await radis .module('module', []) .service('s1', Service1) .run(s1 => { @@ -59,14 +59,14 @@ describe('Module', () => { }) describe('#factory()', function() { - it('should be injected', function() { + it('should be injected', async function() { let module = radis.module('module', []) class Service1 {} class Service2 {} class Service3 {} - module + await module .factory('service1', () => new Service1()) .factory('service2', () => new Service2()) .factory('service3', () => new Service3()) @@ -77,8 +77,8 @@ describe('Module', () => { }) .bootstrap() }) - it('should be injected with $name', function() { - radis + it('should be injected with $name', async function() { + await radis .module('module', []) .factory('s1', $name => $name) .run(s1 => { @@ -86,12 +86,12 @@ describe('Module', () => { }) .bootstrap() }) - it('should throw when nothing is return', () => { + it('should throw when nothing is return', async () => { const module = radis .module('module', []) .factory('s1', function() {}) .run(s1 => {}) - expect(() => module.bootstrap()).toThrow(Error) + await expect(module.bootstrap()).rejects.toThrow(Error) }) it('should check params', function() { expect(() => radis.module('module', []).factory('s1', [function() {}] as any)).not.toThrow(Error) @@ -107,7 +107,7 @@ describe('Module', () => { }) describe('#provider()', function() { - it('should be injected', function() { + it('should be injected', async function() { let module = radis.module('module', []) class Service1Provider { @@ -126,7 +126,7 @@ describe('Module', () => { } } - module + await module .provider('service1', Service1Provider) .provider('service2', Service2Provider) .provider('service3', Service3Provider) @@ -137,7 +137,7 @@ describe('Module', () => { }) .bootstrap() }) - it('should receive the $injector as first constructor parameter', function() { + it('should receive the $injector as first constructor parameter', async function() { class Service1Provider { constructor(public $injector: radis.Injector) {} $get() { @@ -145,13 +145,13 @@ describe('Module', () => { } } - radis + await radis .module('module', []) .provider('s1', Service1Provider) .run(($injector: any, s1: any) => expect(s1).toBe($injector)) .bootstrap() }) - it('should receive $name as second constructor parameter', function() { + it('should receive $name as second constructor parameter', async function() { class Service1Provider { constructor(public $injector: radis.Injector, public $name: string) {} $get() { @@ -159,20 +159,20 @@ describe('Module', () => { } } - radis + await radis .module('module', []) .provider('s1', Service1Provider) .run((s1: any) => expect(s1).toBe('s1')) .bootstrap() }) - it('$get should receive $name as parameter', function() { + it('$get should receive $name as parameter', async function() { class Service1Provider { $get($name: string) { return $name } } - radis + await radis .module('module', []) .provider('s1', Service1Provider) .run(s1 => expect(s1).toBe('s1')) @@ -192,7 +192,7 @@ describe('Module', () => { }) describe('#config()', function() { - it('should call config with the provider', function() { + it('should call config with the provider', async function() { class Service1Provider { private string: string setString(s: string) { @@ -204,7 +204,7 @@ describe('Module', () => { } } - radis + await radis .module('module', []) .provider('service1', Service1Provider) .config(function(service1Provider) {