Can't get the return of valid genre for my supertest

it(‘should return all genre if its valid’, async() =>{

    const res = await exec();

       expect(res.body).toHaveProperty('_id');
       expect(res.body).toHaveProperty('name', 'genre1');
    });

xpect(received).toHaveProperty(path)

Expected path: "_id"
Received path: []

Received value: {}

  118 |         const res = await exec();
  119 |
> 120 |            expect(res.body).toHaveProperty('_id');        
      |                             ^
  121 |            expect(res.body).toHaveProperty('name', 'genre1');
  122 |         });
  123 |     });
1 Like

Could you please share your exec function?

I need to see what is the value of res

This is the exec function

let token;
let name;

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

    beforeEach(() =>{
         token = new User().generateAuthToken();
         name = 'genre1'
    });

I didn’t get your post’s notification because I was neither mentioned nor replied to.
Please reply using the “Reply” button below the user’s post so that that user gets notified.

I was checking my Vidly code and it looks different to what you’ve written.

Mine is as below:

describe('GET /', () => {
	it('should return all genres', async () => {
		await Genre.collection.insertMany([
			{ name: 'genre1' },
			{ name: 'genre2' },
		]);

		const res = await request(server).get('/api/genres');
		expect(res.status).toBe(200);
		expect(res.body.length).toBe(2);
		expect(res.body.some(g => g.name === 'genre1')).toBeTruthy();
		expect(res.body.some(g => g.name === 'genre2')).toBeTruthy();
	});
});

I think the body is an array so you can’t access a property from it. You will be checking its length, and then its content (data stored in the array).

i have a problem with this please. my res.body is an empty object i don’t understand why and status is 404 even though my routes are working correctly and when i run a get request with post man for example i get the genres and everything’s working fine.

You might be using a request type which hasn’t been implemented yet.
That is, you are making DELETE request from the Postman, however delete is not implemented in your route.

If this isn’t the case then please share your code here.

i solved it thank you it was a simple mistake in the path i was making the request to

That’s good. Could you also share the issue and the fix. It could help someone else in the future.

How did you solve this please