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.

Dependency injection inside Unit testing

See original GitHub issue

Hi,

I have two components that need to talk to each other, to avoid the circular dependency issue, I’m using this.moduleRef.get on both components to access the instance methods.

When doing unit test with @nestjs/testing after creating the module and declare both components, the DI doesn’t works:

Scenario 1:

  • Component1 - declares dependency on Component2 via moduleRef
  • Component2 - declares dependency on Component1 via moduleRef
  • server behaviour: works
  • unit test behaviour: fails (both c1 -> c2 & c2 -> c1)

Scenario 2:

  • Component1 - declares dependency on Component2 via moduleRef
  • Component2 - declares dependency on Component1 via constructor
  • server behavour: works
  • unit test behaviour: fails for c1 -> c2 & works for c2 -> c1

So I can’t get it working 😦

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
bariserecommented, Nov 11, 2018

It’s because the testing module is not calling application lifecycle hooks (onModuleInit and onModuleDestroy). You can just call the method manually slightly_smiling_face

Hi, @kamilmysliwiec. Is there any plan to make the testing module call application lifecycle hooks? It would be nice to have that too. PS: Nest is awesome 👍

0reactions
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing and Dependency Injection - The Coders Tower
Unit testing is challenging when you add frameworks around your tests subjects. Dependency injection is one of those patterns supported by ...
Read more >
Is dependency injection essential for unit testing?
Dependency injection allows unit testing, but it also allow modification of an object's behavior without altering the code of that object ...
Read more >
Dependency Injection and Unit Testing - JavaRanch
As its name says, Dependency Injection is all about injecting dependencies, or simply said, setting relations between instances. Some people refer to it...
Read more >
Using DI container in unit tests - Stack Overflow
We've been using constructor injection for all of our production classes, and configuring Simple Injector to populate everything, and ...
Read more >
Favor real dependencies for unit testing - Stack Overflow Blog
If you've worked with unit testing, you've probably used dependency injection to be able to decouple objects and control their behavior ...
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