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

283
Views
why is my test receiving status 200 even though token is not provided?

I defined let token and logged in before each implements, but in this one i simply token = '' to make sure that client is not logged in. I am supposed to get status 401 but instead i am getting 200. Can you tell me what i am doing wrong?

describe('PUT /:id', () => {

        let token;
        let genre;
        let newName;
        let id;

        const exec = async () => {
            return await request(server)
                .put('/api/genres/' + id)
                .set('x-auth-token', token)
                .send({ name: newName });
        }

        beforeEach(async () => {
            genre = new Genre({ name: 'genre1' });
            await genre.save();

            token = new User().generateAuthToken();
            id = genre._id;
            newName = 'updatedName';
        });

        it('should return 401 if client is not logged in', async () => {
            token = '';

            const res = await exec();

            expect(res.status).toBe(401);
        });
 ● /api/genres › PUT /:id › should return 401 if client is not logged in

    expect(received).toBe(expected) // Object.is equality

    Expected: 401
    Received: 200

      142 |             const res = await exec();
      143 |
    > 144 |             expect(res.status).toBe(401);
          |                                ^
      145 |         });
      146 |
      147 |         it('should return 400 if genre is less than 5 characters', async () => {

      at Object.toBe (tests/integration/genres.test.js:144:32)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You might need to show us your route folder containing the endpoint for checking if a user is logged in, and also try to check if your auth middleware function is required on your endpoint.

about 4 years ago · Juan Pablo Isaza 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!