Alon Alon - 4 days ago 3
Node.js Question

Mocha Chai custom comparison function

I'm completely new to Mocha and Chai. I need to test a function named

importDataFromSystem
, which imports data from an external system, manipulates it, and finally stores it in DB.

The object I store in DB is extremely complex, containing thousands of data. I have to compare the whole content in order to make sure the function has succeeded.

In order to do that, I've created a function for the comparison operation:

function compareExtremelyCompexObject (testedObject, trueObject);


Of course in the test I'm not going to actually retrieve data from the external system, but I want to have boilerplate dummy data, as well as boilerplate dummy complex object for the comparison purpose.

My question is: How to write a Mocha Chai code that uses my
compareExtremelyCompexObject
function to execute the test?

Something like:

it('should create a specific complex object from boilerplate data', function(done) {
importDataFromSystem().
.end(function(err, res){
var dummyComplexObject = getBoilerplateComplexObject();
compareExtremelyCompexObject(res, dummyComplexObject);
done();
});
});
});


Notice that
importDataFromSystem
doesn't return anything, it just populates the DB. So how am I supposed to get the complex object it just created?

Also, where should I create the dummy complex object?

The examples I've found until now are missing many essential things like how to test a function rather than a URL, how to compare a complex object (can it be achieved with "should"/"expect"?) and many others. They just exemplify very basic tests, like testing whether a RESTful API result is an array or null, how to check the status code of the HTTP response, and how to make a test that necessitate multiple HTTP requests.

I'll be glad to get either a code match to my specific case, or a reference to a tutorial that contains all of my needs.

Let me know if something is not clear enough. I have really been researching this issue for days. Any help will be profoundly appreciated!

Answer

I think you should edit your question a bit, to simplify, but from what I gather, you want to assert your new object === test object with your custom function? If that's the case, and assuming compareExtremelyCompexObject returns true or false, you're almost there.

it('should create a specific complex object from boilerplate data', function(done) {
  importDataFromSystem()
    .end(function(err, res){
          var dummyComplexObject = getBoilerplateComplexObject();
          // with assert
          assert(compareExtremelyCompexObject(res, dummyComplexObject));
          // or with chai expect
          expect(compareExtremelyCompexObject(res, dummyComplexObject)).to.be.true;
          done();
      });
    });
});

As per your comment, the way you have importDataFromSystem chained, implies that it returns a stream, promise or itself. Let's say its a stream which calls a callback on 'end' then res is likely what you're looking for so the example above should work. However, if res isn't what you're looking for, then you might have to resolve a promise and chain the thenables to ensure a synchronous order of operations. eg.

it('should create a specific complex object from boilerplate data', function(done) {

  Promise.resolve()
    .then(function(){
         return importDataFromSystem();
    })
    .then(function(){
         return assert(compareExtremelyCompexObject(getNewlyCreatedObject(), getBoilerplateComplexObject()));
         // assert should throw error and be caught if the two objects are not equal
    })
    .then(function(){
         done()
    })
    .catch(function(err){ 
         done( err ); 
    });

});

But of course, you need some way to get the object you created. That's a whole other discussion. You should edit your question to narrow down the subject to just dealing with the assertion of a custom comparison. (or risk downvotes, and I'll probs get downvoted by proxy. =] )

Comments