You can do this with the before function, for example ...
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, if you donβt have a really good reason for this, I think you better combine 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 a specific reason why it failed, although there are two claims.
source share