Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
How to create a custom express request to test with supertest

I am trying to call a function from my app using supertest and sending a custom express request but I am getting a 422 Error when I send my request.

This is my custom express request

export interface CreateGroupRequest extends Request {
    body: {
        groupName: string;
        email: string;
        country: string;
    };

This is my mock request

                var testCreateGroupRequest: CreateGroupRequest = {
                   body: {
                        groupName: testName,
                        email: email,
                        country: 'US',
                    }
                } as Request

This is my test so far

 await supertest(app)
                .post("/login")
                .send(testLoginBody)
                .expect(200)
                .then((response) => {
                    sessionToken = response.body.token
                }).then(() => {
                    supertest(app)
                        .post("/create_group")
                        .set('X-JWT-Token', `${sessionToken}`)
                        .send(testCreateGroupRequest)
                        .then((response) => {
                             console.log({response})
                        })
                })

The message that is in the response is "body.groupName\" is required". How should I be creating the custom request?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here's one of supertest's examples:

describe('POST /users', function() {
  it('responds with json', function(done) {
    request(app)
      .post('/users')
      .send({name: 'john'})
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .end(function(err, res) {
        if (err) return done(err);
        return done();
      });
  });
});

Notice that in their send method, they directly send the body. They don't extend or make their own Request.

So to fix your issue you just need to send the body and not the fake request:

supertest(app)
    .post("/create_group")
    .set('X-JWT-Token', `${sessionToken}`)
    .send({
        groupName: testName,
        email: email,
        country: 'US',
    })
    .set('Accept', 'application/json') // Don't forget the header for JSON!
    .then(console.log) // Shortened
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda