question: ServiceNotFound Error in typedi 0.10 (it worked in 0.8.x)

See original GitHub issue

Hi,

I have a problem with tests and new version of typedi. I have an service:

@Service()
export class S3Service {
  @Inject()
  private s3: S3;

and I want test this service. In previous version 0.8 I had working test defined like that:

describe('S3Service', () => {
    let s3Params: any;
    let s3: S3;
    let s3Service: S3Service;

    beforeEach(() => {
      s3 = Container.get(S3);
      s3Service = Container.get(S3Service);
    });

    afterEach(() => {
      Container.reset();
    });

[... tests definitions here ]

Now, in the 0.10 version I see that kind of error:

ServiceNotFoundError: Service with "<UNKNOWN_IDENTIFIER>" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator.

Can someone point me what changed in the 0.10 that caused this problem?

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
NoNameProvidedcommented, Jan 31, 2021

I’m getting the same error where on 0.8 I got S3 class from aws-sdk - what I’m now supposed to do, to have working DI and get S3 instance in my s3 variable?

You have to register it first in the container:

import {S3} from 'aws-sdk';

Container.set({ id: S3, type: S3 });

let s3: S3;

s3 = Container.get(S3);
return;

What does exactly mean in my example? I thought that @Service() mean that the type is registered?

The @Service decorator register your class (S3Service) in the container, not the S3 class from the AWS SDK.

0reactions
github-actions[bot]commented, Mar 4, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot upgrade to 0.10.0 due to @Service() being mandatory ...
Is there any reason why we don't allow like in 0.8.0 ability to instantiate a class without the @Service part? Yes, because attempting...
Read more >
reactjs - TypeDI: class in injected service is not a function or ...
EDIT: Just tried the sample code from the TypeDI page and I'm getting the same exact error: serviceInstance.injectedService.printMessage is not ...
Read more >
typedi - npm
TypeDI is a dependency injection tool for TypeScript and JavaScript. With it you can build well-structured and easily testable applications in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found