Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!