Node.js Mocha Testing Restful API Endpoints and Code Coverage

爷,独闯天下 提交于 2021-01-21 09:21:00

问题


I've been really enjoying Istanbul and experimenting with other Node.js coverage libraries as well, but I have an issue. Nearly all of my unit tests are HTTP calls to my API like so:

    it('should update the customer', function (done) {
        superagent.put('http://myapp:3000/api/customer')
            .send(updatedData)
            .end(function (res) {
                var customer = res.body;
                expect(res.statusCode).to.equal(200);
                expect(customer.name).to.equal(updatedData.name);
                done();
            });
    });

As opposed to actually requiring the customers.js file and calling updateCustomer directly. Testing the endpoint makes much more sense to me, as it not only tests updateCustomer, but also the routes, controllers, and everything else involved.

This works fine, but the problem is that I can't seem to see a way for any code coverage tool to recognize these tests. Is there any way for Istanbul or anything else to recognize these Mocha tests? If not, what is the convention? How do you test endpoints and still use code coverage tools?


回答1:


The issue is that you're using superagent, whereas you should be using supertest to write unit tests. If you use supertest, istanbul will correctly track code coverage.

Some sample code to get you started:

'use strict';

var chai = require('chai').use(require('chai-as-promised'));
var expect = chai.expect;
var config = require('../../config/config');
var request = require('supertest');

var app = require('../../config/express')();

describe('Test API', function () {
  describe('test()', function() {
    it('should test', function(done) {
      request(app)
        .get('/test')
        .query({test: 123})
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res){
          expect(err).to.equal(null);
          expect(res.body).to.equal('whatever');
          done();
        });
    });

    it('should return 400', function(done) {
      request(app)
        .get('/test/error')
        .query({})
        .expect('Content-Type', /json/)
        .expect(400, done);
    });
  });
});


来源:https://stackoverflow.com/questions/31972078/node-js-mocha-testing-restful-api-endpoints-and-code-coverage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!