You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ ] 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:
letproductService: ProductService;letconnection: MockConnection;beforeEach(async()=>{constmodule=awaitTest.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:
module.get(Connection);// correctly infers return type Connectionmodule.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
The text was updated successfully, but these errors were encountered:
I'm submitting a...
Current behavior
In a spec file:
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 withtoken: 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 addingType<any>
to the union) on the TestingModule class, like this:This would allow both patterns to work:
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
The text was updated successfully, but these errors were encountered: