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

Type signature for TestModule.get() is too restrictive #772

Closed
michaelbromley opened this issue Jun 8, 2018 · 2 comments
Closed

Type signature for TestModule.get() is too restrictive #772

michaelbromley opened this issue Jun 8, 2018 · 2 comments

Comments

@michaelbromley
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

In a spec file:

let productService: ProductService;
let connection: MockConnection;

beforeEach(async () => {
    const module = await Test.createTestingModule({
        providers: [
            ProductService,
            { provide: Connection, useClass: MockConnection },
        ],
    }).compile();

    productService = module.get(ProductService);
    connection = module.get<MockConnection>(Connection); // <-- Error: Type 'Connection' is not assignable to type 'MockConnection'.    
});

Expected behavior

I would expect that I could explicitly set the generic argument as MockConnection without error.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

I want to get a reference to the mocked connection instance without resorting to casting to any.

For a comparison, Angular's TestBed.get() is typed with token: any, which I feel is too loose. I like the fact that in Nest, the type is inferred with a generic arg, but for testing, where mocks are the norm, this seems too strict.

I would suggest implementing a slightly looser definition of get() (simply adding Type<any> to the union) on the TestingModule class, like this:

export class TestingModule extends NestApplicationContext {
    // ...
   get<T>(typeOrToken: Type<T> | Type<any> | string | symbol, options?: { strict: boolean; }): T {
       return super.get(typeOrToken, options);
   }
}

This would allow both patterns to work:

module.get(Connection); // correctly infers return type Connection
module.get<MockConnection>(Connection); // returns type MockConnection

If you think this would make sense to add, I would happy to submit this as a pull request. I am building something on top of Nest and I'd be happy to contribute back to this excellent project!

Environment


Nest version: 5.0.1
@kamilmysliwiec
Copy link
Member

Fixed in v5.1.0

@lock
Copy link

lock bot commented Sep 24, 2019

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

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants