Error Cannot find module , testing Nestjs Services

I'm having a hard time with testing services on Nestjs, i believe is something related to my lack of knowledge on how the dependency injection works for tests, weird thing is only getting errors on the test. I have 3 modules Teste, Teste2, Teste3, Teste2 imports Teste3 service, and Teste imports Teste2 service. I tried exporting Teste2 and Teste3, and importing their modules, works fine when i run npm start. Doesnt work on the test thought...

Teste

 @Module({
    imports: [],
    providers: [ TesteService,Teste2Service],
    exports: [TesteService],
    controllers: [TesteController]
  })
  export class TesteModule {}
@Injectable()
export class TesteService {
constructor(private teste2Service: Teste2Service){}

    teste(){
        return this.teste2Service.hello();
    }
}

Teste2

@Module({
  imports: [Teste3Module],
  providers: [Teste2Service],
  exports: [Teste2Service]
})
export class Teste2Module {}
@Injectable()
export class Teste2Service {
    constructor(private teste3Service: Teste3Service){}
    hello(){
        return this.teste3Service.hello();
    }
}

Teste3

@Module({
  providers: [Teste3Service],
  exports: [Teste3Service]
})
export class Teste3Module {}

@Injectable()
export class Teste3Service {

    hello(){
        return 'Hello World';
    }
}

the actual test

describe('TesteService', () => {
  let service: TesteService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports:[Teste2Module],
      providers: [TesteService],
    }).compile();

    service = module.get<TesteService>(TesteService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

The error src/teste/teste.service.spec.ts

Cannot find module 'src/teste2/teste2.service' from 'teste.service.ts'

1 answer

  • answered 2020-04-03 01:43 Vương Nguyễn

    E2E cannot find absolute path. Change to relative path: ../src/teste2