Hey all, looking for some help on how to properly test my JoiValidationPipe for my controller in my controller spec.
Example endpoint:
@Get('login')
@HttpCode(200)
public async login(
@Body(new JoiValidationPipe(LogInUserRequestSchema))
payload: LogInUserRequest,
): Promise<LogInUserResponse> {
return await this.authService.logIn(payload)
}
LogInUserRequestSchema
export const LogInUserRequestSchema = Joi.object({
userName: Joi.string().min(5).max(15).required(),
password: Joi.string().min(5).max(15).required(),
})
The validation pipe
export class JoiValidationPipe implements PipeTransform {
constructor(private schema: Joi.AnySchema) {}
transform(value: any) {
const result = this.schema.validate(value)
if (result.error) {
const errorMessage = result.error.details.map((d) => d.message).join()
throw new BadRequestException(errorMessage)
}
return value
}
}
When I run the following
export const invalidPasswordLoginPayload: LogInUserRequest = {
userName: faker.internet.userName(),
password: '1',
}
it('successfully rejects not valid requests', async () => {
const response = await authController.login(
payloads.invalidPasswordLoginPayload,
)
const { LogInUserData } = response
expect(LogInUserData).toBeUndefined()
})
The test passes as if the validation isn't being picked up.
Any help here would be greatly appreciate!