Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Constructor Injection fail in vitest/jest #1084

Open
zhangfisher opened this issue May 4, 2023 · 3 comments
Open

fix: Constructor Injection fail in vitest/jest #1084

zhangfisher opened this issue May 4, 2023 · 3 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@zhangfisher
Copy link

Description

Constructor Injection fail in vitest/jest
Injected with instance ContainerInstance

import 'reflect-metadata';
//import {expect, test} from "vitest"
import {expect, test} from '@jest/globals';
import { Container, Inject, Service,  ServiceOptions, Token } from 'typedi';
import { FlexEvent } from 'flex-tools'; 
    
@Service({
    id:"app",
    global:true,
    eager:true
})
class App extends FlexEvent{    
    constructor(
        @Inject(WORKSHOP)
        public workshop:Workshop 
    ){
        super()
}
        

@Service({
    id:"app",
    global:true,
    eager:true
})
class App extends FlexEvent{    
    constructor(
        @Inject(WORKSHOP)
        public workshop:Workshop 
    ){
        super()
 //     workshop === Injected with instance ContainerInstance
        
    }
}

test("构造注入", () => {  
    let app = Container.get("app")  as App
    expect(app.workshop).toBeInstanceOf(Workshop)
})

Expected behavior

Injected with instance Workshop

Actual behavior

Injected with instance Workshop ContainerInstance

@zhangfisher zhangfisher added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 4, 2023
@abhayshiravanthe
Copy link

We found out that when you are using Jest and TypeDI, using @Inject tag doesn't actually inject dependencies into the class during test execution. A workaround would be to manually add dependency resolution in the class constructor, for example:

class SomeClass {

  constructor() {
    // Why - for resolvers or classes where Jest can't resolve the injected services
    this.emailService = Container.get(EmailService);
  }

}

@zhangfisher
Copy link
Author

This is not a solution
Due to the inability to inject through construction, if unit testing is required, construction injection can only be abandoned.

@mt3o
Copy link

mt3o commented Jun 27, 2023

The microsofts DI for typescript, tsyringe, can prepare a separate constructor that hides injections within it and wraps the actual constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

3 participants