question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] 6.0.1 Dependency Injection in Testing Module

See original GitHub issue

I’m submitting a…


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

Current behavior

It is not possible to inject dependencies into modules in test scenarios using the overrideProvider mechanism

Expected behavior

It should be possible to inject dependencies into modules in test scenarios using the overrideProvider mechanism

Minimal reproduction of the problem with instructions

https://github.com/WonderPanda/nest-testing-bug Just run yarn test:e2e and you’ll see the following output:

Nest can't resolve dependencies of the BugModule (?). Please make sure that the argument at index [0] is available in the BugModule context.

      at Injector.lookupComponentInExports (../node_modules/@nestjs/core/injector/injector.js:180:19)

Prior to version 6, it was simple to provide mocks for dependencies (both for other providers/controllers as well as modules) using the overrideProvider fluent methods. The change introduced here https://github.com/nestjs/nest/pull/1722/files with an additional check for this.hasProvider(toReplace) seems to have broken those scenarios. It’s a very common scenario in both the libraries I’ve built and the tests that I’ve written to want to be able to supply a provider without it needing to have been previously available in the module context.

The attached repo shows a simple example of this where there is a module that requires a dependency in it’s constructor:

@Module({})
export class BugModule {
  constructor(@Inject('BugProvider') private readonly bugProvider: string) {}
}

but there doesn’t appear to be any mechanism now to be able to achieve this in tests.

I’m a little concerned with the initial launch of version 6 as this and the bug that was fixed as part of 6.0.1 are both core testing scenarios that have come up very often in mine and my team’s use of NestJS. It seems like there’s a gap right now in tests that actually validate that the TestingModule works properly and I’m hesitant to suggest people try version 6 until this is addressed.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Mar 22, 2019

I’ll provide more detailed information shortly 😃

1reaction
apielcommented, Mar 22, 2019

Maybe the documentation is misleading. My first approach for our unit test (isolated tests) was to use catsController = new CatsController(catsService); and not Test.createTestingModule. But because of the documentation, my team convinced me that we should use Test.createTestingModule and I am always in favor to follow the doc. I guess, we need to clarify the doc to make this more understandable. Maybe you could move the Testing utilities section inside of End-to-end testing. Or if you want to keep it after the Unit testing section, you could put a hint “In the most cases, you won’t need Test.createTestingModule for unit testing”. Or something like that, but yeah, writing docs is always super difficult -.-

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection - .NET | Microsoft Learn
Injection of the service into the constructor of the class where it's used. The framework takes on the responsibility of creating an instance...
Read more >
Angular Dependency Injection - @Injectable() fails in test ...
Try with the injector and spyOn. You have to create a mocked service, without the 'HttpClient', that has ALL methods of the Service...
Read more >
New dependency injection features in .NET 6
In this post I talk about some of the new features added to Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.
Read more >
Developer Guide: Dependency Injection - AngularJS: API
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies. The AngularJS injector subsystem is...
Read more >
Testing Dependency Injection • Angular - codecraft.tv
We can resolve dependencies in our tests using a number of methods. We can resolve using the the test bed itself, usually 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