Using Resharper and Karma to run Jasmine tests on Angular modules in the Requirement section

This is a sip.

I feel like asking for the moon, but I would like to use Resharper and Karma to run Jasmine tests for my code, which defines requirements modules containing Angular modules. Running tests with resharper is useful for development - for developers to run interactively. Performing the same tests with karma is useful for continuous integration - automatic test execution.

I was excited when I found this post: Jasmine and Call in Resharper 7 . It shows how to run jaseminejs tests for required modules in resharper. Yes! But it uses named requirejs modules. Named modules are problematic in some situations. In fact, I cannot force karma to perform the same test when naming modules.

For example, here you need a module that defines the 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 the test for this (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 is 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 configuration (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 runs in karma, but resharper cannot run the test. I get errors that "module" and "enter" are not defined. And that makes sense as resharper does not know to download angular -mocks.js.

, resharper require. require(), . , -, .

- , resharper ( angular)?

+4

Source: https://habr.com/ru/post/1529481/


All Articles