Tuesday 16 May 2017

Including external .js files for unit tests

I am trying to run coverage tests for my JavaScript unit tests (written in QUnit [not my idea, legacy project]) and run them via command line. Problem i am facing is references to external bits of code. Look at example bellow:

Code:

var DateHelper = function() {
return {
    GetUtcDate: function (date) {
            if (DateTypeChecker.Data["date"]) {
                return new Date();
            }
            return date;
    }
}    

Test:

QUnit.test('GetUtcNow - compare UTC date', function(assert) {
  var currentUtcDate = DateHelper.GetUtcNow();
  var nowDate = new Date();

  assert.equal(nowDate.getUTCFullYear() == currentUtcDate.getFullYear(), 'dates are the same');
});

So a very simple test that checks two dates, this works lovely when run in the browser, because the external javascript file containing DateTypeChecker is loaded in the HEAD. But this test fails in the command line as node (or w/e is performing the test) doesn't have a reference to DateTypeChecker object.

My question is, how do i fix this, so that the file for DateTypeChecker is loaded/required? (i know that i can do this with RequireJS, but i don't wan't to add more dependencies and frameworks)

I think this is a general question for js unit testing (not just QUnit).



via Aca85

No comments:

Post a Comment