Skizo
Skizo

Reputation: 541

Dependencies when testing in NestJS

As the first example in NestJS shows(https://docs.nestjs.com/fundamentals/testing), the dependency for CatsController is CatsService, so they make new instance of CatService and pass it in the params for CatsController, As easy as pie.

In my real world usage the service is the main service of the system, it depends on a multiple services which they depend on other services and almost all of them including the main one depend on injecting repositories of TypeORM.

How should I handle such of a complex dependency injection when testing?

An example:

Class MainService {
    constructor(
        @InjectRepository(SomeEntity1)
        private readonly someRepo1: Repository<SomeEntity1>,
        @InjectRepository(SomeEntity2)
        private readonly someRepo2: Repository<SomeEntity2>,
        @InjectRepository(SomeEntity3)
        private readonly someRepo2: Repository<SomeEntity3>,
        private readonly someService1: SomeService1,
        private readonly someService2: SomeService2,
        private readonly someService3: SomeService3,
        private readonly someService4: SomeService4,
        private readonly someService5: SomeService5,
        private readonly someService6: SomeService6,
        private readonly someService7: SomeService7,
        private readonly someService8: SomeService8,
        private readonly someService9: SomeService9
    ){}
}

Class SomeService1 {
    constructor(
        @InjectRepository(SomeEntity1)
        private readonly someRepo4: Repository<SomeEntity4>,
        @InjectRepository(SomeEntity2)
        private readonly someRepo5: Repository<SomeEntity5>,
        @InjectRepository(SomeEntity3)
        private readonly someRepo6: Repository<SomeEntity6>,
        private readonly someService4: SomeService4,
        private readonly someService5: SomeService10,
        private readonly someService9: SomeService11
    ){}
}

I could find a solution online. Thanks!

Upvotes: 2

Views: 3936

Answers (1)

GreenMonkeyBoy
GreenMonkeyBoy

Reputation: 834

Generally when you do unit testing, you want to mock the dependencies, because you want to isolate the unit under test from the rest of your application.

Nest offer multiple ways to mock and Jest himself also offers other possibilities (https://jestjs.io/docs/es6-class-mocks).

One way is to use custom providers and provide your own mock:

const module = await Test.createTestingModule({
  providers: [
    MainService,
    {
      provide: SomeService1
      useValue: MockSomeService1
    }
  ]
})
.compile()

MockSomeService1 can be an object, a class or a factory function. Refers to the documentation for more details: https://docs.nestjs.com/fundamentals/custom-providers

If you have a lot of dependencies, that could be a good use case for using auto mocking and https://www.npmjs.com/package/@golevelup/ts-jest combined.

import { createMock } from '@golevelup/ts-jest'

const module = await Test.createTestingModule({
  providers: [
    MainService
  ]
})
.useMocker(() => createMock())
.compile()

More details on auto mocking: https://docs.nestjs.com/fundamentals/testing#auto-mocking

Upvotes: 2

Related Questions