8

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.

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

okay. i've been working on getting testr working for a few days straight now. This is the closest i've come to seeing an example project. I've created a github repo for this example (with some minor tweaks) using jasmine to share with others. However, it is still not working. Can anyone help out here? Thanks. github.com/loesak/jasmine-maven-require-testr
I am having the same problem. In fact, I manually load it as a dep in my require.config, and Karma shows that the file is getting requested (first).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.