I have some code that looks like the following:

var request = require('request');

function Service(){
 this._config = require('../path/to/config.json');

Service.prototype.doThing = function(){
  return new Promise(function(resolve, reject){
    request.post(url, {payload}, function(error, response, body){
      //handle response
    }).on('error', function(err){
      //handle errors

I’m trying to test the block that runs on error, but having difficulty because of the promise. I’m using mocha for test running and sinon for stubbing. I’m able to stub request so that I can count the number of times the on method is called, but the containing Promise never resolves.

There are some packages that work with sinon to handle promises (I’ve tried sinon-as-promised and sinon-stub-promise), but I have to stub the entire doThing method in order for it to resolve properly. I would appreciate any input on the propper way to test this code or alternate code structures that may be easier to test.

The test in question (that hangs waiting for the doThing promise to return) is below:

context('when the server is unavailable', function(){
  beforeEach(function() {
    var onStub = sinon.stub();
    requestStub = {post: function(){ return {on: onStub}}};
    Service = proxyquire('path/to/service', {request: requestStub});
    service = new Service();

  it('should not set the auth key', function(){
    return service.doThing().then(function(x){
      return assert(onStub.calledOnce);