-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix async to service binding resolution (#1635)
* style: update test with right description * fix: update BindingToSyntax.toService() to resolve async dependencies
- Loading branch information
1 parent
af2ca0a
commit 606232e
Showing
4 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { Container, inject, injectable } from '../../src/inversify'; | ||
|
||
describe('Issue 1564', () => { | ||
it('should not throw on getting async services bound using "toService"', async () => { | ||
@injectable() | ||
class Database { | ||
constructor() { | ||
console.log('new Database'); | ||
} | ||
} | ||
|
||
@injectable() | ||
class Service1 { | ||
constructor(@inject(Database) public database: Database) { | ||
console.log('new Service1'); | ||
} | ||
} | ||
|
||
@injectable() | ||
class Service2 { | ||
constructor(@inject(Service1) public service1: Service1) { | ||
console.log('new Service2'); | ||
} | ||
} | ||
|
||
const container: Container = new Container({ defaultScope: 'Request' }); | ||
|
||
container.bind(Database).toDynamicValue(async () => { | ||
console.log('connecting to db...'); | ||
return new Database(); | ||
}); | ||
|
||
container.bind(Service1).toSelf(); | ||
container.bind(Service2).toSelf(); | ||
|
||
container.bind('services').toService(Service1); | ||
container.bind('services').toService(Service2); | ||
|
||
const result: unknown[] = await container.getAllAsync('services'); | ||
|
||
expect(result).to.have.length(2); | ||
}); | ||
}); |