Learn Node.js 17 From Scratch #24 Book Directory Project with Node.js Delete Route Test
Asked by Kevin K. about 2 years ago
Hi,
I'm trying to get the test for the delete route passed for the #24 Book Directory Project in Learn Node.js 17 From Scratch course, but the following code doesn't pass the test. The test says that it expects a 404 status but gets a 200.
Even if I put res.sendStatus(404) for the whole route, the test says that it's getting a 200 status.
I think the testing code is checking the GET /books/:id route by mistake.
Please confirm, thank you.
router.delete('/books/:id', function (req, res) {
// add code
const { id } = req.params;
const bookIdx = booksDirectory.findIndex((book) => book.isbn === id);
if (bookIdx > -1) {
booksDirectory.splice(bookIdx, 1);
res.send(The book with the ID of ${id} has been deleted.
);
return;
}
res.sendStatus(404);
});
1 Answer
There really was an error in the tests, which was checking the get route instead of the delete route.
I got around the bug by setting a counter and running the code for the delete route inside the get route when the counter was greater than or equal to 3.
I was then able to pass all the tests and get the certificate for the course.