microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.16k stars 173 forks source link

Injection of a singleton service inside another singleton is null. #109

Closed handelcamilo closed 4 years ago

handelcamilo commented 4 years ago

Describe the bug Injection of a singleton service inside another singleton is null.

To Reproduce Inject a singleton service inside another singleton.

@singleton() @injectable() class AuthService { constructor(){} }

@singleton() export class UserService { constructor(private authService: AuthService){} }

In this case, 'this.authService' is undefined.

Expected behavior this.AuthService must not be undefined.

Version: "dependencies": { "@react-native-community/masked-view": "^0.1.10", "@react-navigation/native": "^5.5.0", "@react-navigation/stack": "^5.4.1", "react": "16.13.1", "react-native": "0.62.2", "react-native-gesture-handler": "^1.6.1", "react-native-reanimated": "^1.9.0", "react-native-safe-area-context": "^3.0.2", "react-native-screens": "^2.8.0", "reflect-metadata": "^0.1.13", "tsyringe": "^4.3.0" }, "devDependencies": { "@babel/core": "^7.10.2", "@babel/plugin-proposal-decorators": "^7.10.1", "@babel/runtime": "^7.10.2", "@react-native-community/eslint-config": "^1.1.0", "@types/jest": "^25.2.3", "@types/react-native": "^0.62.13", "@types/react-test-renderer": "16.9.2", "@typescript-eslint/eslint-plugin": "^3.1.0", "@typescript-eslint/parser": "^3.1.0", "babel-jest": "^26.0.1", "eslint": "^7.1.0", "jest": "^26.0.1", "metro-react-native-babel-preset": "^0.59.0", "prettier": "^2.0.5", "react-test-renderer": "16.13.1", "typescript": "^3.9.5" },

ps: If I am doing this the wrong way, can you guys please show me how to do it? Thanks!

Xapphire13 commented 4 years ago

@handelcamilo, I can't reproduce this. Tried the following test in global-container.test.ts and it passed:

test("test", () => {
  @singleton()
  class AuthService {
    constructor() {}
  }

  @singleton()
  class UserService {
    constructor(private authService: AuthService) {}

    getAuthService() {
      return this.authService;
    }
  }

  const service = globalContainer.resolve(UserService);

  expect(service.getAuthService()).toBeInstanceOf(AuthService);
});
runia1 commented 2 years ago

For anyone who stumbles across this in the future, I was able to repro this issue, and the fix I discovered is that in your tsconfig.json you need:

"experimentalDecorators": true,
"emitDecoratorMetadata": true,