supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Supertest is casting a number to string?

Open redeemefy opened this issue 2 years ago • 0 comments

I have an endpoint that takes a payload with just one property. That property has a validation rule to be a string and returns a 400 Bad request otherwise. Here is the validation class

import { IsString } from "class-validator";

export class CreateQuestionDto {

    @IsString()
    readonly text: string;
}

In postman, when I hit the endpoint with a payload { "text": 1} I get back

{
    "statusCode": 400,
    "message": [
        "text must be a string"
    ],
    "error": "Bad Request"
}

I have tried two ways to make the test failed

    it.only('Bad request [400]', () => {
      return request(app.getHttpServer())
        .post('/questions')
        .send({ text: 1})
        .expect(201)
    })
// and also
    it.only('Bad request [400]', () => {
      return request(app.getHttpServer())
        .post('/questions')
        .send({ text: parseInt('1', 10)})
        .expect(201)
    })

Both times I get in the console

    Create [POST /questions]
      ✓ Bad request [400] (57 ms)

When the test should be failing. In the case of parseInt('1', 10) Shouldn't be evident for supertest that the value is a number?

redeemefy avatar Jan 25 '22 04:01 redeemefy