Search code examples
node.jsunit-testingmocha.jschaisupertest

Mocha with chai and supertest: expected undefined to equal


I wrote the unit tests:

var app = require('../server');
var chai = require('chai');
var supertest = require("supertest")(app);
var GoogleUrl = require('google-url');
var config = require('../config');

var expect = chai.expect;

describe('Urls Tests', function () {

  var url = {
    author : 'Alexey',
    description : 'grrggr',
    full_url : 'https://github.com',
    date : '30-06-2017',
    time : '18:21:27',
    count_click : 0,
    list_tags : [
      'Sport',
      'Football'
    ]
  };

  var token;

  beforeEach(function (done) {
    agent
      .post('http://localhost:8000/auth/login')
      .send({email: 'Keane95@yandex.ru', password: '123456'})
      .end(function (err, res) {
        if (err) {
          return done(err);
        }

        expect(res.body.userData).to.have.property('token');
        token = res.body.userData.token;
        done();
      });
  });

  it('should create a url', function(done) {

      var googleUrl = new GoogleUrl({
        'key': config.get('google_key')
      });

      googleUrl.shorten(url.full_url, function (err, shortUrl) {

        url.short_url = shortUrl;

        supertest
          .post('/urls/create')
          .send(url)
          .expect(401)
          .end(function (err, res) {
            if (err) return done(err);
            expect(res.body.author).to.equal('Alexey');
            url = res.body;
            done();
          });
      });

    });

  it('should modify a url by id', function(done) {
      url.description = 'Good description';
      url.list_tags.push('Liverpool');
      supertest
        .put('/urls/' + url._id)
        .send(url)
        .expect(401)
        .end(function(err, res) {
          if (err) return done(err);
          expect(res.body.description).to.equal('Good description');
          expect(res.body.list_tags[2]).to.equal('Liverpool');
          done();
        });
    });

  it('should modify a count of clicks', function(done) {
      url.count_click++;
      supertest
        .put('/urls/' + url._id)
        .send(url)
        .expect(401)
        .end(function(err, res) {
          if (err) return done(err);
          expect(res.body).to.equal('Count of the click is updated');
          done();
        });
    });

});

I run to execute the unit tests and get the errors:
enter image description here

I read the articles by unit tests.
First article: http://developmentnow.com/2015/02/05/make-your-node-js-api-bulletproof-how-to-test-with-mocha-chai-and-supertest/
Second article: https://www.codementor.io/olatundegaruba/integration-testing-supertest-mocha-chai-6zbh6sefz

I don't understand why I get these errors. Please, help me. I think that I made little error, but since I cannot fint it.

UPDATED
I added route:

var express = require('express');
var GoogleUrl = require('google-url');
var _ = require('lodash');
var token = require('../middlewares/token');
var Url = require('../models/url');
var config = require('../config');
var router = express();

router.post('/create', token.required, createShortUrl);
router.put('/count/:id', token.required, updateCountClick);
router.put('/:id', token.required, updateUrlById);

module.exports = router;

function createShortUrl(req, res) {

    _.trim(req.body.list_tags);
    var tags = _.split(req.body.list_tags, ',');
    tags.splice(tags.length - 1, 1);

    var date = returnDate();
    var time = returnTime();

    var googleUrl = new GoogleUrl({
        'key': config.get('google_key')
    });

    googleUrl.shorten(req.body.full_url, function (err, shortUrl) {

        if (err) {
            res.status(500).json(err);
        }

        var url = new Url({
            'author': req.payload.username,
            'description': req.body.description,
            'full_url': req.body.full_url,
            'short_url': shortUrl,
            'list_tags': tags,
            'date': date,
            'time': time
        });

        url.save(function (err, url) {

            if (err) {
                return res.status(500).json(err);
            } else {
                return res.status(200).json(url);
            }

        });

    });

}

function updateCountClick(req, res) {

    var count_click = req.body.count_click + 1;

    Url.findOneAndUpdate({_id: req.params.id}, {$set: {count_click: count_click}}, {new: true}, function (err, url) {

        if (err) {
            return res.status(500).json(err);
        }

        if (url) {
            return res.status(200).json('Count of the click is updated');
        }

    });
}

function updateUrlById(req, res) {

    _.trim(req.body.list_tags);
    var tags = _.split(req.body.list_tags, ',');
    tags.splice(tags.length - 1, 1);

    Url.findOneAndUpdate({_id: req.params.id}, {$set: {description: req.body.description, list_tags: tags}}, {new: true}, function (err, url) {

        if (err) {
            res.status(500).json(err);
        }

        if (url) {
            res.status(200).json(url);
        }

    });

}

UPDATED 2

Authoziration was added:

var token;

beforeEach(function (done) {
    agent
      .post('http://localhost:8000/auth/login')
      .send({email: 'Keane95@yandex.ru', password: '123456'})
      .end(function (err, res) {
        if (err) {
          return done(err);
        }

        expect(res.body.userData).to.have.property('token');
        token = res.body.userData.token;
        done();
      });
  }); 

Also I updated code my unit-tests.


Solution

  • I can't see where in your code you send 401 and Url. So it seems that your test requests are getting rejected by token.required middleware with 401 status code (which means "unauthorized").

    .send(url)
    .expect(401) // why do you expect 401? You never send it inside your logic
    

    So basically your test never hit actual code.

    First of all, you do need to fake authorization to make token.required middleware happy.

    Then expect 200 result

    .send(url)
    .expect(200) // normal execution flow of createShortUrl results in 200
    .end(/* rest of your test logic */)