Async functions and chai testing

For my issue tracker project, I created a middleware function that retrieves a specific project issue. It all works well. However my functional-tests are failing with:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/app/tests/2_functional-tests.js)

I’m not sure where I am supposed to call done() as I am calling it already.

.put(getIssue, async function(req, res){ ... }

async function getIssue(req, res, next) {
        let project;
        try {
          project = await Project.findOne({'project_title':req.params.project})
          if (project == null) {
              return res.status(404).json({ message: "Cannot find project" }) // 404 is cannot find something
          } else {
              for (let i of project.issues) {
                if (i._id == req.body._id) {                
                  // variable on the response object so that we can use  'res.issue'  in every route
                  res.issue = i;
                }      
              }
          }          
        } catch (error) {
          return res.status(500).json({ message: error.message })
        }
      // next moves us on to next function
      next()    
    }
      test('One field to update', function(done) {
        chai.request(server)
        .put('/api/issues/test')
        .send({
          _id: '5d7698b981262723ffbc2095',
          created_by: "functional test - One field to update"
        })
        .end(function(err, res){
          assert.equal(res.status, 200);
          //console.log(res);
          assert.equal(res.body.message, 'successfully updated', '== successfully updated') 
          done();
        });
      }); 
1 Like

Try setting the timeout to a value higher than 2000ms, for example:

      test('One field to update', function(done) {
        chai.request(server)
        .put('/api/issues/test')
        .send({
          _id: '5d7698b981262723ffbc2095',
          created_by: "functional test - One field to update"
        })
        .end(function(err, res){
          assert.equal(res.status, 200);
          //console.log(res);
          assert.equal(res.body.message, 'successfully updated', '== successfully updated') 
          done();
        });
      }).timeout(5000); // add timeout 5000ms here 
1 Like

This is good for me to review this thread.