0

I tried following the documentation here as best i could, but im getting an error where the DI seems broken. What am i missing

Here is the error, with some logs:

stdout | src/resources/cats/cat.spec.ts > Cats
beforeAll

stdout | src/resources/cats/cat.spec.ts > Cats
app initialized

stdout | src/resources/cats/cat.spec.ts > Cats > /GET cats
catsService undefined

[Nest] 34132  - 09/29/2025, 5:41:37 PM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'findOne')
TypeError: Cannot read properties of undefined (reading 'findOne')
    at CatsController.findOne (/Users/user/code/dvmonorepo/apps/backend/src/resources/cats/cat.controller.ts:16:29)

module:

import { Module } from '@nestjs/common';
import { CatsService } from './cat.service';
import { CatsController } from './cat.controller';

@Module({
  controllers: [CatsController],
  providers: [CatsService],
  exports: [CatsService],
})
export class CatsModule {}

controller:

import { Controller, Get } from '@nestjs/common';
import { CatsService } from './cat.service';

@Controller('cats')
export class CatsController {
  constructor(
    private readonly catsService: CatsService,
  ) {}

  @Get(':catId')
  findOne() {
    console.log('catsService', this.catsService);
    return this.catsService.findOne('1');
  }
}

service:

import { Injectable } from '@nestjs/common';

@Injectable()
export class CatsService {
  constructor() {}

  async findOne(id: string) {
    return { id, email: '[email protected]', name: 'Test Cat' }
  }
}

spec:


import request from 'supertest';
import { Test } from '@nestjs/testing';
import { CatsModule } from './cat.module';
import { CatsService } from './cat.service';
import { INestApplication } from '@nestjs/common';

describe('Cats', () => {
  let app: INestApplication;
  let catsService = { findOne: () => ['test'] };

  beforeAll(async () => {
    console.log('beforeAll');
    const moduleRef = await Test.createTestingModule({
      imports: [CatsModule],
    })
      .overrideProvider(CatsService)
      .useValue(catsService)
      .compile();

    app = moduleRef.createNestApplication();
    await app.init();
    console.log('app initialized');
  });

  it(`/GET cats`, () => {
    return request(app.getHttpServer())
      .get('/cats/1')
      .expect(200)
      .expect({
        data: catsService.findOne(),
      });
  });

  afterAll(async () => {
    await app.close();
  });
});

some extra words down here because stack overflow thinks i posted too much code lol. blah blah blah. Please let me know if you need more information.

1 Answer 1

0

Although i have not determined why yet, this issue can be fixed by using string tokens. Ive double checked that i dont have a conflicting name, so it may be caused by somthing in my monorepo setup or caching

import { Module } from '@nestjs/common';
import { CatsService } from './cat.service';
import { CatsController } from './cat.controller';

@Module({
  controllers: [CatsController],
  providers: [
    {
      provide: 'CatsService', // Use string token
      useClass: CatsService,
    },
  ],
  exports: ['CatsService'],
})
export class CatsModule {
  constructor() {
    console.log('CatsModule: Initialized with providers =', CatsService);
  }
}
// --- controller ----
import { Controller, Get, Inject } from '@nestjs/common';
import { CatsService } from './cat.service';

@Controller('cats')
export class CatsController {
  constructor(@Inject('CatsService') private readonly catsService: CatsService) {
    if (!this.catsService) {
      console.error('CatsController constructor: WARNING - CatsService is undefined!');
    }
  }

  @Get(':catId')
  findOne() {
    console.log('findOne: catsService =', this.catsService);
    return this.catsService.findOne('1');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.