nodejs test app

Solutions on MaxInterview for nodejs test app by the best coders in the world

showing results for - "nodejs test app"
Lisa
13 Apr 2018
1// index.js
2
3var express = require('express'); // (npm install --save express)
4var request = require('supertest');
5
6function createApp() {
7  app = express();
8
9  var router = express.Router();
10  router.route('/').get(function(req, res) {
11    return res.json({goodCall: true});
12  });
13
14  app.use(router);
15
16  return app;
17}
18
19describe('Our server', function() {
20  var app;
21
22  // Called once before any of the tests in this block begin.
23  before(function(done) {
24    app = createApp();
25    app.listen(function(err) {
26      if (err) { return done(err); }
27      done();
28    });
29  });
30
31  it('should send back a JSON object with goodCall set to true', function() {
32    request(app)
33      .get('/index')
34      .set('Content-Type', 'application/json')
35      .expect('Content-Type', /json/)
36      .expect(200, function(err, res) {
37        if (err) { return done(err); }
38        callStatus = res.body.goodCall;
39        expect(callStatus).to.equal(true);
40        // Done
41        done();
42      });
43  });
44
45});
46