Reputation: 11
I'm new to Nest.js, and I've been working on tests for my AuthController, and I keep getting this error, why is that?
`Nest can't resolve dependencies of the AuthService (?, UserService, JwtService). "Please make sure that the argument UserModel at index [0] is available in the AuthModule context."
Potential solutions:
- Is AuthModule a valid NestJS module?
- If UserModel is a provider, is it part of the current AuthModule?
- If UserModel is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing UserModel */ ]
})`
Here's the test module config for auth.controller.spec.ts:
const UserModel = getModelForClass(User);
describe('AuthController', () => {
let app: INestApplication;
let authService: AuthService;
beforeEach(() => {
jest.setTimeout(30000);
});
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AuthModule,
UserModule,
MongooseModule.forRootAsync({
useFactory: async () => {
const mongo = await MongoMemoryServer.create();
const uri = mongo.getUri();
return {
uri,
useNewUrlParser: true,
useUnifiedTopology: true,
};
},
}),
],
providers: [AuthService, UserService, JwtService, UserModel],
}).compile();
app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
await app.init();
authService = module.get<AuthService>(AuthService);
});
Here's the auth.module.ts file:
@Module({
imports: [
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET_KEY,
signOptions: { expiresIn: '30m' },
}),
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, LocalAuthGuard, JwtAuthGuard],
exports: [JwtAuthGuard],
})
export class AuthModule {}
And here's the UserModule:
const UserModel = getModelForClass(User);
@Module({
imports: [
MongooseModule.forFeature([
{
name: User.name,
schema: UserSchema,
},
]),
],
providers: [UserService, UserModel],
exports: [UserModel],
})
export class UserModule {}
Upvotes: 0
Views: 98
Reputation: 601
The error message suggests that the AuthService
has dependencies on UserModel
, UserService
, and JwtService
, but Nest.js is unable to resolve these dependencies. This could be due to a few reasons:
UserModel
is not a valid provider: Make sure that UserModel
is a valid provider
in the current module or in a module that is imported by the current module. You can check if UserModel
is a valid provider by checking if it is decorated with @Injectable()
.
UserModel
is not part of the current module: If UserModel
is exported
from a separate module, make sure that the module is imported by the current module. In this case, UserModel
is exported
from the UserModule
, so make sure that UserModule
is imported
by the AuthModule
.
UserService
and JwtService
are not provided in the current module: Make sure that UserService
and JwtService
are provided in the current module or in a module that is imported by the current module. In this case, UserService
and JwtService
are not provided in the AuthModule
, so you need to add them to the providers array
in the AuthModule
.
So, the solution is,
AuthModule
that includes UserService
and JwtService
in the providers array:
@Module({
imports: [
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET_KEY,
signOptions: { expiresIn: '30m' },
}),
UserModule, // make sure to import UserModule
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, LocalAuthGuard, JwtAuthGuard, UserService, JwtService],
exports: [JwtAuthGuard],
})
export class AuthModule {}
Thank you.
Upvotes: 0