Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript dependency injection using RequireJS, Jasmine and testr

I've just looking for dependency injection in my unit test strategy using RequireJS and Jasmine. I really like the idea behind testr and I have tried to setup testr following the examples in github but I can't figure out what is wrong. I always get the error

Error: module has not been loaded: today

when testr tries to load the module that is going to be tested.

Here some context..

index.html ..

<script data-main="config" src="../../assets/js/libs/require.js"></script>
<script src="vendor/testr.js"></script>

config.js ..

require.config({

  // Initialize specs.
  deps:["main"],
...
...
});

main.js ..

require([
  // Load the example spec, replace this and add your own spec
  "spec/today"
], function() {
  var jasmineEnv = jasmine.getEnv();
  jasmineEnv.execute();
});

spec\today.js ..

describe('Today print', function() {
  var date = {}, today;
  beforeEach(function() {
     date.today = new Date(2012, 3, 30);
     today = testr('today', {'util/date': date});  //Here is where the error is thrown
  });

  it('is user-friendly', function() {
     expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012');
  });
});

today.js ..

define(['string', 'util/date'], function(string, date) {
  return {
    getDateString: function() {
      return string.format('Today is %d', date.today);
    }
  }
});

Is there anybody that have been with the same kind of trouble? . I'm using RequireJS 2.0.6

Thanks.

like image 455
aschiavoni Avatar asked Sep 20 '12 19:09

aschiavoni


1 Answers

Your 'today' module needs to be loaded from requirejs before you use it with testr. Try something like:

require(['today'], function(){
    describe('Today print', function() {
      var date = {}, today;
      beforeEach(function() {
         date.today = new Date(2012, 3, 30);
         today = testr('today', {'util/date': date});  //Here is where the error is thrown
      });

      it('is user-friendly', function() {
         expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012');
      });
    });
});

Also read: http://cyberasylum.janithw.com/mocking-requirejs-dependencies-for-unit-testing/

like image 125
janith Avatar answered Nov 15 '22 08:11

janith