#Best practices for testing/mocking a badly coded monster project

24 messages · Page 1 of 1 (latest)

heady skiff
#

I'm asking for recommendations (hoping again my savior jmcdo29 will come xD - did not mention on purpose 🙏 )

I'm currently at a company where we adopted nest, but the learning curve made the firsts services we have quite a mess.

i have opinions on what/how to test. we have tests which are creating a test module and importing each and every dependency (and sub dependency) in order to test a single service.

where i would rather import the module and pull the tested service from testingModule.get , optionally providing mocks with the dedicated overrideModule/overrideProvider functions.

some of the test files i have, they have 20~ services imported, where actually 2 modules would be enough... but the dependencies are a bit broader than currently.

i'm wondering, in a world of newbies of nest what is the preferred way?

#

otherwise said:

do you recreate the whole application dependency graph in a single TestingModule as a flat module containing all the providers in a single providers[]

OR

do you actually import the modules and mock the parts you need to mock individually.

viral scaffoldBOT
#

There's a large repository of samples managed by the community.

heady skiff
#

thanks a lot for the command

#

@bleak hatch (i'm mentioning you only bc you answered 🙏 ) from a quick look, the most complex example (named complex-sample) seems really under in terms of "mess"

bleak hatch
#

It's definitely not built to be a mess 😉

#

My opinion is that in tests, imports shouldn't be used

heady skiff
#

hahaha if i could go back in time and get hired earlier i wouldn't be reviewing this

#

i see.

#

so no modules at all and rather reproduce the whole structure of an app in a separate testing module?

bleak hatch
#

Are these units tests or integration/e2e tests?

heady skiff
#

they're supposed to be unit tests, but there is quite big entanglement

bleak hatch
#

If they're unit, then yes, just providers, no modules

heady skiff
#

i see

#

what if a dependency of the tested service needs to pull... 3 services, which also pull 3 services (given 2 or 3 levels)?

#

still add all manually in providers?

bleak hatch
#

That's why you mock the dependencies of the service you're trying to test, so you don't have to worry about the depth of the providers

heady skiff
#

mmm then that's the bit i missed i think (even though saying this out loud does seem like obvious)

#

thanks for bringing clearness to my eyes 😄

#

that's clear as crystal. (i still dont know how to mark a thread as done but it's done)

#

as usual @bleak hatch you're amazing

viral scaffoldBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.

vast ravine
#

Yeah, the problem is if you don't mock out all the dependencies, then your unit tests end up testing another service's functionality. E2E tests are for testing all services together. Unit tests should be mocking out everything except the class under test.

mint hedge