Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Chai/Mocha/Should: running multiple tests against the same response

I'm using something very similar to the following to execute a series of API tests using Mocha. This is great, but it requires making a separate API call for each test. I want to use the same API call and run multiple tests against that response. I've been reading that you can use before to do it, but none of the examples around the web actually show it working with API calls?

var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
    expect = chai.expect,
    should = chai.should();

describe('/', function () {
  it('should return 200', function (done) {
    request.get('http://localhost:8000', function (err, res, body) {
      res.should.have.status(200);
      done();
    });
  });

  it('should say "Hello, world!"', function (done) {
    request.get('http://localhost:8000', function (err, res, body) {
      body.should.have.property('type', 'aType');
      done();
    });
  });
});
like image 205
brandonscript Avatar asked Nov 24 '14 01:11

brandonscript


People also ask

Do Mocha tests run sequentially?

Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

What is the difference between Mocha and chai?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.


1 Answers

You could do this with a before function like so...

var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
    expect = chai.expect,
    should = chai.should();

describe('/', function () {
  var firstRequest;

  before(function(done) {
    request.get('http://localhost:8000', function(err, res, body) {
      firstRequest = {
        err:err,
        res:res,
        body:body
      };
      done();
    });
  });

  it('should return 200', function (done) {
    firstRequest.res.should.have.status(200);
    done();
  });

  it('should say "Hello, world!"', function (done) {
    firstRequest.body.should.have.property('type','aType');
    done();
  });
});

However, unless you have a really good reason to do this, I think you're better off just combining the tests.

var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
    expect = chai.expect,
    should = chai.should();

describe('/', function () {
  it('should return 200 and say "Hello, world!"', function (done) {
    request.get('http://localhost:8000', function (err, res, body) {
      res.should.have.status(200);
      body.should.have.property('type', 'aType');
      done();
    });
  });
});

If the test fails Mocha will report the specific reason why it failed even though there are two assertions.

like image 86
Daniel Avatar answered Oct 15 '22 22:10

Daniel