Using mockjax with mocha

254 Views Asked by At

I am experimenting with building a very simple API using express and would like to use Mocha for testing. I also want to use jquery-mockjax to mock third-party API calls but not sure how to go about that. I used npm install to install Mocha globally, and jquery and jquery-mockjax within the app.

How do I go about requiring mockjax and the xmldom library (to mock xml response returned by third-party API)? Code wise, here's how I imagined it would look like but doesn't work:

var request = require('supertest');
var should = require('should');
var $ = require('jquery');
var constants = require('../lib/constants');

require('jquery-mockjax');
// require('/opt/orbit/orbit-api/node_modules/jquery-mockjax/lib/jquery.xmldom.js');

var server = request.agent("https://localhost:8080/api");

describe("As a user, I", function() {

  beforeEach(function() {
    this.timeout(0); //disables mocha 2 second timeout limit
  });

  it("should be able to sign in", function(done) {
    $.mockjax({
      url: apiUrl + 'auth/signin',
      type: 'post',
      responseXML: '<tsResponse><credentials token="1234" ><site id="1" contentUrl="url" /><user id="1" /></credentials></tsResponse>'
    });

    server
      .post("/auth/signin")
      .send({
        username: username,
        password: password
      })
      .expect("Content-type", /json/)
      .end(function(err, res) {
        res.body.http_code.should.equal(200);
    done();
  });
});
0

There are 0 best solutions below