mardi 30 avril 2019

Test a simple express app: Uncaught ReferenceError: done is not defined

I tried to test a simple express app by following this tutorial.

After running mocha test.js Error I got is: done is not defined (see below)

enter image description here

Full code here

var express = require('express'); // (npm install --save express)
var request = require('supertest');
var app;

function createApp() {
  app = express();

  var router = express.Router();
  router.route('/').get(function(req, res) {
    return res.json({ goodCall: true });
  });

  app.use(router);

  return app;
}

describe('Our server', function() {
  // Called once before any of the tests in this block begin.
  before(function(done) {
    app = createApp();
    app.listen(function(err) {
      if (err) {
        return done(err);
      }
      done();
    });
  });

  it('should send back a JSON object with goodCall set to true', function() {
    request(app)
      .get('/index')
      .set('Content-Type', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, function(err, res) {
        if (err) {
          return done(err);
        }
        const callStatus = res.body.goodCall;
        expect(callStatus).to.equal(true);
        // Done
        done();
      });
  });
});

Aucun commentaire:

Enregistrer un commentaire