karma-runner / karma-commonjs

A Karma plugin. Test CommonJS modules.
MIT License
73 stars 30 forks source link

karma-commonjs does not work along with angularjs mock module #23

Closed akoshelev closed 10 years ago

akoshelev commented 10 years ago

Hi,

I tried to use this module with application that built around AngularJS framework and faced a problem. I write unit tests using Mocha and my specifications look like this:

describe('Myservice', function () {

    beforeEach(module('myModule', ['$provide', function ($provide) {
        ...
    });
    ...
});

After loading karma-commonjs plugin, karma fails to run tests with the following error:

Uncaught TypeError: object is not a function

I understand why it happens: karma-commonjs modifies file contents as follows:

window.__cjs_modules_root__ = "basePath";
window.__cjs_module__ = window.__cjs_module__ || {};
window.__cjs_module__["myServiceSpec.js"] = function(require, module, exports) {
    describe('Myservice', function () {

        beforeEach(module('myModule', ['$provide', function ($provide) {
            ...
        });
        ...
    });
}

This modification breaks reference to angular.module - module variable now points to CommonJS module, not to AngularJS mock module. So it seems to me that there is a name conflicting issue. Can we address this issue or there is only one possible solution not to use module, but to use angular.module instead?

egor-smirnov commented 10 years ago

One of solutions - don't use karma-commonjs preprocessor for your test files. Will it work for your case?

pkozlowski-opensource commented 10 years ago

@akoshelev the angular-mocks.js file is not written as a CommonJS module, so I'm not sure why you are trying to pass it through CommonJS pre-processor. Is there is a particular reason for doing so?

@lastday is right here - this file shouldn't go through the CommonJS pre-processor as it doesn't require any CommonJS modules and doesn't export any CommonJS modules.

egor-smirnov commented 10 years ago

@akoshelev you could also try to call 'angular.mock.module()' instead of 'module()' as they are the same.

akoshelev commented 10 years ago

@lastday Yes, I think this is the only one possible solution, thanks.