Echo JS 0.11.0

<~>
2color 1569 days ago.
I'm working on a large js application which is mostly written using Backbone/Marionette. Right now I am using Grunt with an array that has the correct order along with uglify to minify the JS files. 

This setup is working mostly fine. A bit tedious having to figure out the correct order of files, but I manage. 

My goal is to introduce automated testing of the application.

considering how much cross referencing I have and dependency on the global namespace to access object instances, I thought it would first make sense to modularize the code ( using require.js )

I know this can be a tough refactor but I'm wondering how important this is, if I want to start using some testing libraries to test the code.

radubrehar 1568 days ago. link 2 points
If you are concerned about testing and using requirejs, I would suggest you have a look at the karma test runner, which works nicely with jasmine and requirejs. I have just recently integrated it into one of my projects and having a real-time execution of tests really boosts your productivity and confidence in your codebase.

See http://karma-runner.github.io
amatiasq 1569 days ago. link 1 point
No tool is really important, it's just useful. It's not very known but RequireJS allows you to load files on diferent "contexts", I use this to create one context per test suite so in this context I can mock specific dependencies and they are not loaded.

I use "Multiversion" API to create one context per suite and "map" config parameter to mock dependencies:

http://www.requirejs.org/docs/api.html#config-map

http://www.requirejs.org/docs/api.html#multiversion


    requirejs.config({
      map: {
        'models/user': {
          'srv/auth': 'test/mocks/srv/auth'
        };
      };
    });

    define(function(require) {
      // if "../models/users" ask for module "srv/auth" require will delivery "test/mocks/srv/auth" instead
      var userModel = require('../models/user');
      // test
    });
bevacqua 1569 days ago. link 1 point
I wouldn't waste a ton of time setting up Require.

Like you said, it involves a lot of work, and the only upside you get is not having to declare the order of your dependencies, which you already have to begin with.

This doesn't have anything to do with testing, though. How would Require benefit your testing efforts?

In my case I use Angular, which handles the dependency ordering for me, so I don't have to worry about that kind of thing.

Adding the files is then just a matter of setting up Grunt to pick up any `*.js` file, bundle them, and minify.