'HTTP POST request test is not getting passed
I wrote some nodejs codes to be evaluated by a particular test file, candidate.test.js but surprisingly the test keeps failing. Below is the error I am getting:
- Error: expect(received).toEqual(expected) // deep equality
- Expected: 201
- Received: 500
I have below the snippet from both app.js and candidate.test.js.
candidate.test.js
const request = require('supertest');
const app = require('../app');
// ...
async function makeRegularGetRequest(requestBody) {
return await request(app)
.post('/restaurant')
.send(requestBody)
.expect('Content-Type', /json/);
}
it('should create a restaurant given valid parameters', async () => {
const requestBody = {
name: '123',
position: 200,
category: 'Pizza',
rating: 5,
};
const createResponse = await makeRegularPostRequest(requestBody);
try {
expect(createResponse.status).toEqual(201);
} catch (error) {
throw error;
}
});
// ...
app.js
// ...
app.post('/restaurant', (request, response) => {
const id = generateRandomString();
const { name, position, category, rating } = request.body;
const restaurant = {
name: name,
position: position,
category: category,
rating: Number(rating),
id: id,
};
restaurants.push(restaurant);
response.send(restaurant).status(201);
});
// ...
I also tried sending only the statusCode to the page using reponse.send(restaurant).status(201).statusCode
, but the error still comes out.
I tested this on Postman and it works really fine but it's strange that the test does not pass.
Solution 1:[1]
can you console log before and after pushing to restaurants array
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tarek Salah |