Testing

Like @nestjs/mongoose (see the testing section) nestjs-typegoose's forFeature and forRoot rely on a database connection to work. To unit test your CatService without connecting to a mongo database you need to create a fake model using a custom provider.

import { getModelToken } from "nestjs-typegoose";
@Module({
ProductService,
{
provide: getModelToken('Product'),
useValue: fakeProductModel
}
})

In a spec file this would look like:

const fakeProductModel = jest.fn();
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: getModelToken("Product"),
useValue: fakeProductModel
},
ProductService
]
}).compile();

The string given to getModelToken function should be the class name of the typegoose model that you are testing.