4

That's a mouthful.

I feel like I'm asking for the moon, but I would like to be able to use Resharper and Karma to run my Jasmine tests for my code that defines Require modules that contain Angular modules. Running the tests via resharper is useful for development -- for developers to run interactively. Running the same tests via karma is useful for continuous integration -- automated test execution.

I got excited when I found this post: Jasmine and Requirejs in Resharper 7. It shows how to run jaseminejs tests for require modules in resharper. Yeah! But it leverages named requirejs modules. Named modules are problematic in some situations. In fact, I cannot get karma to run the same test when the modules are named.

For example, here's a require module that defines an angular module (app/stringCalculator.js):

define(['angular'], function () {
    angular.module('stringCalculatorModule', []).factory('stringCalculator', function() {
        return {
            calculate: function (string) {
                var result = 0;
                string.split("+").forEach(function(number) {
                    result += parseInt(number);
                });
                return result;
            }
        };
    });
});

And a test for that (Tests/stringCalculatorSpec.js):

/// <reference path="../Lib/require.js" />
describe('string calculator', function () {
    var modulesLoaded;

    beforeEach(function () {
        if (!modulesLoaded) {
            require(['stringCalculator'], function () {
                modulesLoaded = true;
            });
            waitsFor(function () {
                return modulesLoaded;
            }, 'loading external module', 1000);
            runs(function () {
                module('stringCalculatorModule');
            });
        }
    });

    it('should add 1 and 2', function () {
        inject(function (stringCalculator) {
            var result = stringCalculator.calculate('1+2');
            expect(result).toEqual(3);
        });
    });
});

Here my requirejs config (require.conf.js):

requirejs.config({
    // Note: Karma serves files from '/base'
    baseUrl: '/base/App',
    paths: {
        angular:        '/base/Lib/angular',
        ngMocks:        '/base/Lib/angular-mocks',
        jQuery:         '/base/Lib/jquery',
    },
    shim: {
        ngMocks:        { deps: ['angular'] },
    },
    deps: function() {
        var items = Object.keys(window.__karma__.files).filter(function(file) {
            return /Spec\.js$/.test(file);
        });
        items.push('ngMocks');
        return items;
    }(),
    callback: window.__karma__.start
});

And my karmajs config (karma.conf.js):

module.exports = function (config) {
    config.set({
        autoWatch: true,
        browsers: ['PhantomJS'],
        frameworks: ['requirejs', 'jasmine'],
        files: [
            'require.conf.js',
            { pattern: 'Tests/**/*.js', included: false },
            { pattern: 'App/**/*.js', included: false },
            { pattern: 'Lib/**/*.js', included: false },
        ],
    });
}

This code does run in karma, but resharper cannot run the test. I get errors that 'module' and 'inject' are not defined. And that makes sense since resharper doesn't know to load angular-mocks.js.

Then again, I'm not sure resharper will work with an unnamed require module. Even if I pass a nonsense name to require(), I don't get an error. I don't think it's loading anything even though there's no error.

Does anyone know how to get resharper and karma to run the same tests (that use require and angular)?

Community
  • 1
  • 1
steve
  • 1,021
  • 1
  • 14
  • 29
  • have you solved that? – Eugene P. Jun 04 '14 at 12:37
  • yes, but I do not like the solution. It works if I use the relative path to the script as the name of the module. I don't like this since it's too easy to mess up -- actually file name/path different than module name. – steve Jun 04 '14 at 17:49
  • have you tried that one? https://github.com/StarterSquad/angularjs-requirejs-seed . it also has dependency on proxy, but it's best I have found, at the present got very similar issue – Eugene P. Jun 05 '14 at 13:44

0 Answers0