Skip to content

Instantly share code, notes, and snippets.

@steelx
Created February 14, 2022 10:36
Show Gist options
  • Save steelx/246005d8b2471bd4fe41b791e5cc88ac to your computer and use it in GitHub Desktop.
Save steelx/246005d8b2471bd4fe41b791e5cc88ac to your computer and use it in GitHub Desktop.
Type-di demo
import 'reflect-metadata';
import {Container, Inject, Service} from "typedi";
describe('typedi demo', () => {
it('should work', async () => {
const query = () => 'hello from query';
@Service()
class Repository {
constructor(@Inject('q') private q: () => string) {
}
public getHello(): string{
return this.q();
}
}
@Service()
class ExampleService {
constructor(private helloRepo: Repository) {
}
public getHello(): string{
return this.helloRepo.getHello()
}
}
Container.set('q', query);
const service = Container.get(ExampleService)
expect(service.getHello()).toBe("hello from query")
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment