Issue tracker test

why this test is not working , issue tracker project

.end(function(err, res){
, 'created_on');
, 'updated_on');
, 'issue_title');
, 'created_by');
, 'assigned_to');
, 'status_text');
, '_id');
          assert.equal(res.status, 200);
          assert.equal(res.body.issue_title, 'Title');
          assert.equal(res.body.created_by, 'Functional Test - Every field filled in');
          assert.equal(res.body.assigned_to, 'Chai and Mocha');
          assert.equal(res.body.status_text, 'In QA');
          assert.equal(, true);


  Functional Tests

    POST /api/issues/{project} => object with issue data

      1) Every field filled in

      2) Required fields filled in


It doesn’t work because you’re not responding to the request. Compare your post route with the get one; in the latter, you’re responding with res.json(data), whereas the others don’t have any.

Review the route /api/issues/:project methods (post, put and delete) and make sure you’re responding with something.

One more thing:

if (err) {

Is fine, but if there’s actually an error, you will be answering with the same response regardless. What you should do is:

if (err) {
  return res.status(500).json({ error: 'Database operation failed' });
// Here you process the successful response
1 Like

but I dont need to pass anything in
.post(function(req, res) {
.put(function(req, res) {

I changed the res but still getting same output

I see… the problem you have now is that you’re not awaiting for the save to finish, hence the newIssue may or may not be saved when you send the response.

You should use async/await for all your database calls:

.post(async function(req, res) {
  // Removed the rest of the code just to make it clearer
  newIssue = new Issue(newIssue);
  // When using async/await, you need to add try/catch blocks
  // to handle errors, otherwise they may leak information
  // to the client. You could catch them later though.
  try {
    // After saving it, the newIssue will have an _id
    newIssue = await;
  } catch (error) {
    console.error('Error saving issue:', error);
    return res.json({ error: 'Could not save the issue' });

One thing I didn’t mention is that async/await is not the only option, since you can pass a callback or use Promises.

Here are examples for doing the same as before with Promises and callbacks:

Using Promises

.post(function(req, res) {
  newIssue = Issue(newIssue);
  .then(savedIssue => res.json(savedIssue))
  .catch(e => {
    console.error('Error saving issue:', e);
    return res.json({ error: 'Could not save the issue' });

Using Callbacks

// This is the one you may know best
.post(function(req, res) {
  newIssue = Issue(newIssue);, savedIssue) => {
    if (error) {
      console.error('Failed to save issue:', error);
      return res.json({ error: 'Could not save the issue' });
1 Like

yeah did that ,
still having issue in last test

    test("Valid _id", function(done) {
        .end(function(err, res) {
        //assert statement 



the object returned is empty

Yes, but that’s expected :stuck_out_tongue:. Look at your delete method:

    _id: deleteId
}, (err, done) => {
    if (err) {

Here you’re just returning deleted as a string, not JSON (which is OK, since that’s what the project requires). See here (note the content-type):

Remember that the test requires:

I can DELETE /api/issues/{projectName} with an _id to completely delete an issue. If no _id is sent return ‘_id error’. Return ‘Deleted {_id}’ if you successfully delete an issue, and ‘Could not delete {_id}’ if you could not.

Hence your delete method should return Deleted ID, where ID is the req.body._id.

Try to fix the issues and let us know how it goes :slight_smile:.

1 Like

yes the remove function was removed from mongoose thats also one reason

That’s OK :stuck_out_tongue:. The actual deletion of the record is fine, what’s wrong is what you’re returning and what you’re testing.

You should return _id error if the req.body._id is not present and Deleted ${req.body._id} when the record is deleted.

In regards to the tests, you should test the entire body, not as an object:

// Inside your test:
assert.equal(res.body, `Deleted ${id}`)

:thinking: currenty I’m doing

 assert.equal(res.text,"deleted","error in delete")
Issue.deleteOne({ _id: deleteId }, (err, done) => {
          if (err) {

isn’t that right ?

Honestly, I’m not sure what the property res.text will contain. Does it fail the test?

If you change .text to .body should work though (if it’s not working now, of course :stuck_out_tongue:).

res.text will contain deleted

working :muscle:

1 Like

@Advitya-sharma, now produce messages for other methods/actions, delete is working