#End-to-end testing example with a module that imports MongooseModule

16 messages · Page 1 of 1 (latest)

tough spruce
#

I am trying to make the documentation example work with a module that imports MongooseModule.

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

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

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

    app = moduleRef.createNestApplication();
    await app.init();
  });

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

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

This is my CatsModule

@Module({
  imports: [MongooseModule.forFeature([{name: Cats.name,  schema: CatsSchema,}])],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}

This is the error that I get
Nest can't resolve dependencies of the CatsModel (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.

So a couple of questions here

  • why is Nest requiring the CatsModel if CatsController doesn't use it and the CatsService is mocked?
  • if it's just because of the imports array, how do I mock it so it stops requiring the DatabaseConnection too? I am certain I don't want to use either in my tests.
celest otter
#

If you have a MongooseModule.forFeature() you must have a MongooseModule.forRoot() to set up the database connection

#

If you don't plan to actually plan to use the database, then use .overrideProvider(getModelToken(Cat.name)).useValue(catModelMock) before you call .compile()

tough spruce
#

what does catModelMock look like?

celest otter
#

It looks like a Model<Cat> instance.

#

Do you plan on mocking the database in your e2e test?

tough spruce
#

I don't plan to use it at all

celest otter
#

Then why are you setting up e2e tests? Just to be able to hit the endpoint?

tough spruce
#

yeah, to check if the validation and guards work

celest otter
#

Rather than imports: [CatModule], you could define the controllers and providers, and then you don't have toworry about the mongoose model mock in the first place

tough spruce
#

this is the closest example i could find for my use-case

celest otter
#
Test.createTestingModule({
  controllers: [CatController],
  providers: [
    {
      provide: CatService,
      useValue: catsService
    }
  ]
})
tough spruce
#

in this case I still setup the app as in the above example and ping using request()?

celest otter
#

Yep

#

This can still setup an entire application, there's no need to use imports to do that as this does set up a RootTestModule under the hood that will be used when it comes to setting up the server. Then it's jsut as beforemodRef.createNestApplication() and app.init()/app.listen(0)

tough spruce
#

it works, thank you!