#How to include Validation Pipes in controller tests?

15 messages · Page 1 of 1 (latest)

tall dagger
#

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!

tall dagger
#

Ohp got it
Wasn't providing it

 const app: TestingModule = await Test.createTestingModule({
      controllers: [AuthController],
      providers: [AuthService, AuthDbService, JoiValidationPipe],
    }).compile()
 it('successfully rejects not valid requests', async () => {
    const { error } = LogInUserRequestSchema.validate(
      payloads.invalidPasswordLoginPayload,
    )

In case this helps anyone

strange epoch
#

Just be aware that by invoking the controller methods directly like this and not throght a http request, the enhancers (guards, interteptors, pipes) don't trigger.

tall dagger
#

Yeah that makes sense.
Is there any documentation regarding how to properly test a controller though an http request?

granite saddle
#

The e2e part of the testing docs

tall dagger
#

Thank you!

primal tangle
#

I am currently stuck on an issue where I can trigger my controller via postman to properly catch and throw the bad request exception as expected, but I have tried a few different ways to implement a test on the controller for unit testing code coverage and it always states the pipe is un triggered. any suggestions? @granite saddle @strange epoch

granite saddle
#

How are you attempting to trigger the pipe in your unit test?

primal tangle
#

one moment i will add screen shots

#

This is the controller for reference

granite saddle
#

As I said before, direct calls to the controller won't trigger the pipe, you need it to be a request sent through the rest of the API, i.e. via http calls

primal tangle
#

🤔 ok. ill see if i can piece it together with the previously mentioned docs, that is not something I have done before

primal tangle
#

Do you have any other examples of this besides the docs?