In case anyone runs into this issue in the future, I got repeated failures for the 2 tests in test.js testing the POST /api/meetings route.
After troubleshooting with some logs, I found that the api call in test.js did not include any payload and this caused the db validation to fail:
// No payload sent in the request.
// Just including the first test here for brevity but it's also missing
// from the second one.
it('should create a new meetings and return it', function() {
return request(app)
.post('/api/meetings')
.expect(201)
.then((response) => response.body)
.then((createdMeeting) => {
expect(createdMeeting).to.have.ownProperty('time');
expect(createdMeeting).to.have.ownProperty('date');
expect(createdMeeting).to.have.ownProperty('day');
expect(createdMeeting).to.have.ownProperty('note');
});
});
I updated the tests to include a fake meeting and the tests passed:
// Import createMeeting function from db.js
const { createMeeting } = require('../server/db.js');
// Create fake meeting to use in the payload
const fakeMeeting = createMeeting();
// Add .send(fakeMeeting) into the call chain
it('should create a new meetings and return it', function() {
return request(app)
.post('/api/meetings')
// Added this line:
.send(fakeMeeting)
.expect(201)
.then((response) => response.body)
.then((createdMeeting) => {
expect(createdMeeting).to.have.ownProperty('time');
expect(createdMeeting).to.have.ownProperty('date');
expect(createdMeeting).to.have.ownProperty('day');
expect(createdMeeting).to.have.ownProperty('note');
});
});
// Same change for the second test:
it('should persist the created meeting to the database', function() {
let initialMeetingsArray;
let newlyCreatedMeeting;
return request(app)
.get('/api/meetings')
.then((response) => {
initialMeetingsArray = response.body;
})
.then(() => {
return request(app)
.post('/api/meetings')
// Added this line:
.send(fakeMeeting)
.expect(201);
})
.then((response) => response.body)
.then((createdMeeting) => {
newlyCreatedMeeting = createdMeeting;
return request(app)
.get('/api/meetings')
})
.then((response) => response.body)
.then((newMeetingsArray) => {
expect(newMeetingsArray.length).to.equal(initialMeetingsArray.length + 1);
let createdMeetingFound = newMeetingsArray.some((meeting) => {
return meeting.id === newlyCreatedMeeting.id;
});
expect(createdMeetingFound).to.be.true;
});
});
If I am mis-understanding how these tests worked, and you believe they should have passed without this change, please let me know, Good learning oppourtunity!