Node.js Mocha Testing Other API Endpoints and Code Coverage

I really enjoyed Istanbul and experimented with other Node.js coverage libraries, but I have a problem. Almost all of my unit tests are HTTP calls for my API:

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(); }); }); 

Unlike the actual request of the customers.js file and a direct call to updateCustomer . Endpoint testing makes a lot more sense to me, because it's not only updateCustomer tests, but also routes, controllers, and everything else.

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

+4
source share
1 answer

The problem is that you are using superagent , while you have to use supertest to write unit tests. If you use supertest , istanbul correctly tracks code coverage.

Sample code to run:

 '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); }); }); }); 
+3
source

All Articles