zayquan
zayquan

Reputation: 8062

karma-browserify with angularjs unit tests: name collision on 'module' causes 'object is not a function' error

I have an angular app that I am testing with karma unit tests and I am attempting to add karma-browserify so that I can require npm modules in my unit tests.

Once I added the browserify preprocessor to my karma.conf.js config file my tests began to break, even though I have not even tried to require any modules yet.

I get the following error:

object is not a function

If I follow the trace I see that the line causing the error is in the browserify bundled js temp file on a call to 'module'

Upvotes: 2

Views: 1264

Answers (1)

zayquan
zayquan

Reputation: 8062

Just saw an older post with same question so I answered it there as well:
angular mock `module` resulting in '[Object object] is not a function'

Change the call to load the app in the unit test from:

beforeEach(module("app"))

to:

beforeEach(window.angular.mock.module("app"))

This is a documented issue here which appears unlikely to be fixed:
https://github.com/xdissent/karma-browserify/issues/10 I hope this doesn't cost you a day like it did me.

Upvotes: 8

Related Questions