How to override provider for tests in NestJs and not create transitive dependencies that are not needed? - nestjs

I am writing end-to-end tests in NestJs and using the "overrideProvider" function to setup test versions of various services (e.g. services that require database connections). I noticed though that even when I do this, the original implementation that injects the real database is still instantiated.
Is there a way to tell Nest to not create transitive dependencies that are overridden?
For example, I have a test that starts like:
...
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [ServiceModule],
})
// Works if I uncomment these lines:
// .overrideProvider('Database')
// .useValue(new TestDatabase())
.overrideProvider('ServiceUsingDatabase')
.useValue(new TestService())
.compile();
...
Where the module setup is like:
import { Inject, Injectable, Module } from '#nestjs/common';
interface Database {}
#Injectable()
class ProductionDatabase implements Database {
constructor() {
throw('Cannot create a production database.');
}
}
#Injectable()
export class TestDatabase implements Database {
constructor() {
console.log('Creating the test database.');
}
}
#Module({
providers: [
{
provide: 'Database',
useClass: ProductionDatabase
}
],
exports: ['Database']
})
class DatabaseModule {}
interface Service {}
#Injectable()
class ProductionService implements Service {
constructor(#Inject('Database') private readonly database: Database) {}
}
#Injectable()
export class TestService implements Service {
// Test implementation of the service does not Inject anything.
}
#Module({
imports: [DatabaseModule],
providers: [
{
provide: 'ServiceUsingDatabase',
useClass: ProductionService
}
],
})
export class ServiceModule {}
But, the DI system is still seeming to try and instantiate ProductionDatabase. If I explicitly override the provider for the 'Database' it works, but I'd like to avoid having to explicitly list all transitive dependencies as such.

I ended up deciding to make a "Test" Module for every Module e.g.:
import { Inject, Injectable, Module } from '#nestjs/common';
interface Database {}
#Injectable()
class ProductionDatabase implements Database {
}
#Injectable()
export class TestDatabase implements Database {
}
#Module({
providers: [
{
provide: 'Database',
useClass: ProductionDatabase
}
],
exports: ['Database']
})
class DatabaseModule {}
#Module({
providers: [
{
provide: 'Database',
useClass: TestDatabase
}
],
exports: ['Database']
})
class TestDatabaseModule {}
interface Service {}
#Injectable()
class ProductionService implements Service {
constructor(#Inject('Database') private readonly database: Database) {}
}
#Injectable()
export class TestService implements Service {
}
#Module({
imports: [DatabaseModule],
providers: [
{
provide: 'ServiceUsingDatabase',
useClass: ProductionService
}
],
})
export class ServiceModule {}
#Module({
providers: [
{
provide: 'ServiceUsingDatabase',
useClass: TestService
}
],
})
export class TestServiceModule {}
etc... Though it turned out after some refactorings that the "Test" module wasn't needed as some Modules became pure business logic.

Related

Nest js convert module from forRoot to forRootAsync

#Module({
imports: [],
providers: [SupertokensService, AuthService],
exports: [],
controllers: [AuthController],
})
export class AuthModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
static forRoot({
connectionURI,
apiKey,
appInfo,
}: AuthModuleConfig): DynamicModule {
return {
providers: [
{
useValue: {
appInfo,
connectionURI,
apiKey,
},
provide: ConfigInjectionToken,
},
],
exports: [],
imports: [],
module: AuthModule,
};
}
}
The problem with this implementaion I can't use env variables, so I need useFactory to pass ConfigService. Can somebody do that, and give some explanation.
I figure out how to make this work, unfortunately it only works with nest.js version 9 (current latest version). First you need to create a new file. For example auth.module-definition.ts. Now in this file we need to create ConfigurableModuleBuilder.
import { ConfigurableModuleBuilder } from '#nestjs/common';
import { AuthModuleConfig } from './config.interface';
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
new ConfigurableModuleBuilder<AuthModuleConfig>()
.setClassMethodName('forRoot')
.build();
we need to set setClassMethodName('forRoot'), when we set forRoot it will create two methods, forRoot and forRootAsync. The next step is to extend our created ConfigurableModuleClass. it should look something like this
import { MiddlewareConsumer, Module } from '#nestjs/common';
import { AuthMiddleware } from './auth.middleware';
import { SupertokensService } from './supertokens/supertokens.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { ConfigurableModuleClass } from './auth.module-definition';;
#Module({
imports: [],
providers: [SupertokensService, AuthService],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule extends ConfigurableModuleClass {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
}
And that's actually it, so from now on we have forRootAsync and we get reigister it in app.module.ts
import { MiddlewareConsumer, Module, NestModule } from '#nestjs/common';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigType } from '#nestjs/config';
import authConfig from './auth/auth.config';
#Module({
imports: [
AuthModule.forRootAsync({
inject: [authConfig.KEY],
imports: [ConfigModule.forFeature(authConfig)],
useFactory: (config: ConfigType<typeof authConfig>) => {
return {
connectionURI: config.CONNECTION_URI,
appInfo: {
appName: config.appInfo.APP_NAME,
apiDomain: config.appInfo.API_DOMAIN,
websiteDomain: config.appInfo.WEBSITE_DOMAIN,
apiBasePath: config.appInfo.API_BASE_PATH,
websiteBasePath: config.appInfo.WEBSITE_BASE_PATH,
},
};
},
})
],
controllers: [],
providers: [
],
})
export class AppModule implements NestModule {
}
here I am using Nest.js Config, but you don't need to, so use it how you want.
I know that my english is not the best, so if you still do not understand you can check these sources https://docs.nestjs.com/fundamentals/dynamic-modules#configurable-module-builder
https://trilon.io/blog/nestjs-9-is-now-available#Configurable-module-builder

Dependency Injection in Nestjs Service classes

I have two modules whose services are dependent on one another.
// organization.module.ts
//...imports
#Module({
imports: [
forwardRef(() => EmployeeModule),
TypeOrmModule.forFeature([
OrganizationRepository,
Employee
])
],
controllers: [OrganizationController],
providers: [
OrganizationService
]
})
export class OrganizationModule {}
Organization Service*
#Injectable()
export class OrganizationService {
constructor(
#InjectRepository(OrganizationRepository)
private orgRepository: OrganizationRepository,
#InjectRepository(Employee)
private employeeRepsoitory: Repository<Employee>,
private readonly employeeService: EmployeeService,
){}
//...
}
Then i have my employee module and service
employee.module.ts
#Module({
imports: [
forwardRef(() => OrganizationModule),
//...
]
controllers: [EmployeeController],
providers: [EmployeeService, JwtStrategy],
exports: [EmployeeService],
})
export class EmployeeModule {}
and then i have my employee service
employee.service.ts
#Injectable()
export class EmployeeService {
constructor(
private readonly organizationService: OrganizationService
) {}
Now i am getting this DI error and i dont know what is going wrong here. I have tried
#Inject(forwardRef(() => Service)) but didnt seem to work.
My app.module.ts looks like
//...
#Module({
imports: [
OrganizationModule,
],
controllers: [AppController],
providers: [
AppService,
],
})
export class AppModule {}
What am i doing wrong?
You are missing an exports in OrganizationModule.
Add to OrganizationModule:
exports: [OrganizationService]

Dynamic ConfigModule cannot Inject in custom JwtModule

I created my Dynamic configModule to extract environment variables from a different path. It extracts from an yml file. Everything works properly if a add in some module. Here is my ConfigModule:
import { DynamicModule } from '#nestjs/common';
import { ConfigModule } from '#nestjs/config';
import { EnvConfigService } from './env.config.service';
export class EnvConfigModule {
/**
* Create an static function to call directly from the class without instantiation
* #param options: Our config module attributes or properties
* #returns DynamicModule
*/
static register(options): DynamicModule {
return {
module: ConfigModule,
providers: [
{
provide: 'CONFIG_OPTIONS',
useValue: options,
},
EnvConfigService,
],
exports: [EnvConfigService],
};
}
}
Now when I want to add that configuration in the new custom JwtModule, CustomJwtModule:
...
import { EnvConfigModule } from 'src/utils/environment/env.config.module';
import { EnvConfigService } from 'src/utils/environment/env.config.service';
#Module({
imports: [
JwtModule.registerAsync({
imports: [EnvConfigModule],
inject: [EnvConfigService],
useFactory: (configService: EnvConfigService) => {
const base64_pubKey = configService.get(ACCESS_PUBLIC_KEY_NAME);
return {
publicKey: Buffer.from(base64_pubKey, KEY_ENCODING),
signOptions: {
algorithm: ENCRYPTION_ALGORITHM,
},
};
},
}),
],
providers: [
{
provide: JWT_ACCESS_SERVICE,
useExisting: JwtService,
},
],
exports: [JWT_ACCESS_SERVICE],
})
export class JWTAccessModule {}
And here is my error:
Error: Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument EnvConfigService at index [0] is available in the JwtModule context.
I guess, I do get that error because it does not inject properly in JWT module my service.
My app.module.ts is implemented in that way
...
import { EnvConfigModule } from './utils/environment/env.config.module';
#Module({
imports: [
IamModule,
EnvConfigModule.register({
folder: 'config/environment/',
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Ok, it seems I was importing wrongly. It was missing some part of the configuration. Here is the working example of CustomJwtModule:
...
import { EnvConfigModule } from 'src/utils/environment/env.config.module';
import { EnvConfigService } from 'src/utils/environment/env.config.service';
#Module({
imports: [
JwtModule.registerAsync({
imports: [EnvConfigModule.register({
folder: 'config/environment/',
}),
],
inject: [EnvConfigService],
...

NestJS 8 own AuthGuard & Passport: Cannot read property 'authenticate' of undefined

I have own implementation of AuthGuard:
import { authenticate } from 'passport';
#Injectable()
export class AuthGuard implements CanActivate {
canActivate(context: ExecutionContext){
...
authenticate(...);
}
}
When I call authenticate method I get: Cannot read property 'authenticate' of undefined at Authenticator.authenticate (.../node_modules/passport/lib/authenticator.js:162:26
It looks like Passport hasn't been started.
AuthModule:
#Module({
imports: [PassportModule],
providers: [
AuthGuard,
{
provide: APP_GUARD,
useClass: AuthGuard,
},
],
})
export class AuthModule {}
and AppModule:
#Module({
imports: [
AuthModule
]
})
export class AppModule {}
This code was working in Nest 7.
Do you have any idea what could be wrong?

NestJS : cannot provide an instance to child module owning the interface

I try to decouple the controller module of a NestJs module from a domain module (as per clean architecture principles), so that dependency flows only to Domain.
I do not understand how to make it work, injection of the repo interface's implementation does not happen (error when nest starts).
Here is the module structure :
AppModule -> CommunityControllerModule -> DomainModule
(provides repoImpl (has service requiring an unknown
by token) concrete repo interface impl)
The CommunityControllerModule :
import { DomainModule } from 'libs/domain/src';
import { Module } from '#nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
#Module({
imports: [DomainModule, CommunityRepositoryImpl],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}
The DomainModule :
import { Module } from '#nestjs/common';
import { CommunityService } from './community.service';
#Module({
providers: [CommunityService],
exports: [CommunityService],
})
export class DomainModule {}
The CommunityService where injection fails :
import { HttpException, HttpStatus, Inject, Injectable } from '#nestjs/common';
import { CommunityRepository } from './community.repo';
#Injectable()
export class CommunityService {
constructor(
#Inject('CommunityRepository') private repo: CommunityRepository,
) {}
}
The outputted error :
> nest start
webpack 5.28.0 compiled successfully in 8218 ms
[Nest] 7204 - 2021-05-05 20:52:05 [NestFactory] Starting Nest application...
[Nest] 7204 - 2021-05-05 20:52:05 [InstanceLoader] CommunityRepositoryImpl dependencies initialized +49ms
[Nest] 7204 - 2021-05-05 20:52:05 [ExceptionHandler] Nest can't resolve dependencies of the CommunityService (?). Please make sure that the argument CommunityRepository at index [0] is available in the DomainModule context.
Potential solutions:
- If CommunityRepository is a provider, is it part of the current DomainModule?
- If CommunityRepository is exported from a separate #Module, is that module imported within DomainModule?
#Module({
imports: [ /* the Module containing CommunityRepository */ ]
})
What I missed was to set the DomainModule as a dynamic module.
The Domain module becomes :
import { DynamicModule, Module } from '#nestjs/common';
import { CommunityService } from './community.service';
#Module({})
export class DomainModule {
static register(repoImpl): DynamicModule {
return {
module: DomainModule,
imports: [repoImpl],
providers: [
CommunityService,
{
provide: 'CommunityRepository',
useClass: repoImpl,
},
],
exports: [CommunityService],
};
}
}
...and the CommunityControllerModule imports it with the repo implementation :
import { DomainModule } from 'libs/domain/src';
import { Module } from '#nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
#Module({
imports: [
DomainModule.register(CommunityRepositoryImpl),
CommunityRepositoryImpl,
],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}

Resources