Nestjs Module import not mantaining the imported module configuration

Viewed 21

I have this module:

import { HttpModule } from '@nestjs/axios'
import { Module } from '@nestjs/common'
import { ApiService } from './api.service'

@Module({
  imports: [
    HttpModule.register({
      headers: {
        'API-KEY': process.env.API_KEY,
        'API-PASSPHRASE': process.env.API_PASSPHRASE
      }
    })
  ],
  providers: [ApiService],
  exports: [ApiService]
})
export class ApiModule {}

But then if I import that module somewhere else like:

import { ApiModule } from '@/api/api.module'
import { Module } from '@nestjs/common'
import { SomeRepository } from './repository/some.repository'
import { SomeRepositoryAdapter } from './repository/some-repository.adapter'
import { CqrsModule } from '@nestjs/cqrs'

@Module({
  imports: [CqrsModule, ApiModule],
  providers: [
    {
      provide: SomeRepository,
      useClass: SomeRepositoryAdapter
    }
  ],
  exports: [SomeRepository]
})
export class SomeModule {}

When I try to run a test for SomeModule :

import { Test, TestingModule } from '@nestjs/testing'
import { SomeModule } from './some.module'
import { SomeRepository } from './repository/some.repository'

describe('SomeModule', () => {
  let someService: SomeRepository

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [SomeModule]
    }).compile()

    someService = module.get<SomeRepository>(SomeRepository)
  })


  it('Should do something', async () => {
    const someResult = await someService.someMethodThatCallsApiService('some')
    console.log(someResult)
    expect(someResult).toBeTruthy()
  })
})

In the call to someService.someMethodThatCallsApiService I lost the headers configured with HttpModule.register on ApiModule, is there a way to prevent this from happening or I'm setting these modules in the wrong way?

0 Answers
Related