11

I have written a NodeJS app using express that proxies some calls to external APIs. So I am trying to write a unit test using Mocha and Sinon. My goal is to test the app without any internet connectivity so I am trying to mock the https requests and return mock replies.

I'm having a problem that I can't find any examples or tutorials that fit my case. My node app listens on port 8081 for http requests and then proxies them to another site. I want to test my app without it having to actually send the request to those external servers. I'm trying it below and I put the json replies I want to send back in the server.respondsWith() function.

Am I doing this the right way by making an ajax call with chai? or should I be sending the requests inside my app somehow. Any help is appreciated.

var assert = require('assert');
var chai = require('chai');
var spies = require('chai-spies');
var chaiHttp = require('chai-http');
var https = require('https');
var should = chai.should();
var expect = chai.expect;
var sinon = require('sinon');

chai.use(spies);
chai.use(chaiHttp);

describe('Car Repository', function() {
  var server;
  before(function() {
    server = sinon.fakeServer.create();
  });

  after(function() {
    server.restore();
  });

  var url = 'http://127.0.0.1:8081';
  it('should succeed and return a list of cars', function(done) {
    server.respondWith('POST', 'https://api.sandbox.cars.com/v2/token_endpoint', JSON.stringify({"access_token":"1t3E4IykfpJAbuFsdfM2oFAo5raB5vhfOV0hAYe","token_type":"bearer","expires_in":604800}));
    server.respondWith('GET', url+'/cars', JSON.stringify({'test':'this works'}));

    chai.request(url)
      .get('/cars')
      .end(function(err, res) {
        if (err) {
          throw err;
        }

        res.should.have.status(200);
        res.body.should.have.property('test');
        console.log(res.body);

        done();
      });
    });
});
george_h
  • 1,562
  • 2
  • 19
  • 37

3 Answers3

21

Check out the Nock library. It does exactly what you're looking for.

Nock is an HTTP mocking and expectations library for Node.js

Nock can be used to test modules that perform HTTP requests in isolation.

For instance, if a module performs HTTP requests to a CouchDB server or makes HTTP requests to the Amazon API, you can test that module in isolation.

Community
  • 1
  • 1
KJ3
  • 5,168
  • 4
  • 33
  • 53
0

The new solution here is sinon's fake server:

http://sinonjs.org/releases/v2.1.0/fake-xhr-and-server/#fake-server

danday74
  • 52,471
  • 49
  • 232
  • 283
  • This is a dead link since 2018.. Here's an updated link: https://sinonjs.org/releases/v15/fake-xhr-and-server/ – Tzahi Leh Aug 13 '23 at 07:44
0

Take a look at node-tdd and the useNock flag. It builds on top of mocha and nock (mentioned in the accepted answer) and automatically creates and uses a recording file for each test.

We love that it's so easy to use. Basically just "enable and forget" and focus on writing requests / test cases. If requests for a test change, one still needs to delete or adjust the recording file, but at least it's entirely separate from the code.

vincent
  • 1,953
  • 3
  • 18
  • 24