Karma + AngularAMD: troubleshooting error "angularAMD not initialized." - angularjs

Struggling here, spent hours on this already. Karma test keeps saying:
Error: angularAMD not initialized. Need to call angularAMD.bootstrap(app) first.
Take a look at the build on runnable.com. Hit Run button or type gulp in the terminal to run the karma test. Should see the error as shown above.
Anyone want to take a stab at figuring out where I went wrong? I'm already calling angularAMD.bootstrap(app) in test/app.js so I don't know why karma can't see it.

Solution available via: https://github.com/marcoslin/angularAMD/issues/103
Updated runnable.com app with working examples.

Related

Karma Istanbul fix to work with new version of Chrome

In my AngularJs application I'm using Jasmine and Karma for my unit tests.
Recently, after a chrome update (now on 72.0.3626.81) my unit tests starting failing locally (with no change to code). I believe the error is a result of my coverage tool Instanbul.
Sometimes the error appears like this:
An error was thrown in afterAll Uncaught ReferenceError: __cov_iuQO6FdumXRPLjSMopb0JQ is not defined thrown
Other times it will appear within a specific unit test (not sure why).
I searched my application and the only file this __cov_ variable could come from is the return of a function within the Instanbul package, generateTrackerVar() within instrumenter.js.
As no code changes were made I assume that the issue is with the new version of Chrome, perhaps the security settings.
My question what permissions would karama-coverage/istanbul require? OR if anyone suspects the issue isn't security based, then what could be causing this error?
Thanks
EDIT:
I have tried to disable web security in my gulpfile like so:
browsers: [ 'Chrome_without_security' ],
customLaunchers: {
Chrome_without_security: {
base: 'Chrome',
flags: ['--disable-web-security']
}
},
But this didn't solve the issue. Is there a way to set the chrome version in this config?
I managed to solve this by figuring out the hashcode after __cov_ was related to a test file that was throwing an error. I don't know why the error didn't just appear as it normally would, that will be another problem to solve.
For now I managed to figure out the file by logging in instrumenter.js within the instanbul package. Then by removing my coverage tool I figured out the source of the issue. Simply removing the coverage tool would give me a different error but not tell me which file so I needed to do both.

'Uncaught TypeError: angular.module(...).run(...) is not a function' in Grunt minified js-file

My code works on development mode, uncompressed. But when I use grunt's minify functionality and run the app in production mode, I get that error: Uncaught TypeError: angular.module(...).run(...) is not a function.
When I look into the error in Google Chrome Developer Tools, I get what you see in the screenshot. Does anybody understand where the problem is?
I am using Angular 1.4+ in Angular-Fullstack. The error occurs in vendor.{{randomstring}}.js.
Thanks for any support!
You have 2 issues here.
Dependency injection. You can edit manually the archives or use https://github.com/mgol/grunt-ng-annotate this will do this automagically for you.
Concatenation. Check this grunt plugin https://github.com/gruntjs/grunt-contrib-concat the issue here is javascript dont understand when a function end/start because dont have a separation mark, check where the error is.
})(function(a,b) {
It should be:
});(function(a,b) {
Check the "Concatenating with a custom separator" example of grunt-contrib-concat

Running more than 10 tests on karma using jasmine causes: "ERROR: Some of your tests did a full page reload!"

So, this is my first project where I'm using Karma and Jasmine to unit test my angularJS code. Used Yeoman angular generator for the setup.
As soon as I reached 11 tests, I got an error saying "Some of your tests did a full page reload". I'm not doing any tests that would trigger a reload.
Digging deeper I saw the same issue being referenced on Github.
https://github.com/jasmine/jasmine/issues/366 -- (FuzzySockets comments)
The problem seems to stem from a line of code in jasmine-core
https://github.com/jasmine/jasmine/blob/master/lib/jasmine-core/jasmine.js
To avoid overflow of stack, the maximumSpecCallbackDepth is set to 20. And every time currentSpecCallbackDepth exceeds that, further tests are executed on a new stack by using the setTimout function.
This is the line that seems to cause problems and makes karma throw the error. (I've verified this by invoking the setTimeout method in my own unit test, and it threw the same error).
If change the maximumSpecCallbackDepth to 100, my tests run fine, and no errors are thrown at the end
Has anyone seen this issue and know a fix? I'm using the latest versions of karma(0.13.15) and jasmine(2.4.1).
I haven't really messed around too much with the default grunt or karma config that came with yeoman generated ones, except that I'm using the chrome launcher instead of the default phantomJS, so I don't understand how everyone else is not facing the same issue here.
+1 for this issue. As u said, it caused by maximumSpecCallbackDepth limitation, but I didn't find no fix for this issue so far. You probably could track issue here https://github.com/karma-runner/karma/issues/1101 .
One temporary solution is to reduce nested 'describe' block in your project.
I got a similar issue where the angular injections in the global beforeEach stopped working and all tests failed after the 20 limit of maximumSpecCallbackDepth.
During my investigations, I found out that angular-mock doesn't play well with the setTimeout done in jasmine when that limit is reached.
The following code that is given as example everywhere will create a new injector on each test case:
var yourService;
beforeEach(module('app'));
beforeEach(inject(function(_yourService_) {
yourService = _yourService_;
}));
Instead, you could do the following, which will use a single injector and register your modules only once.
var yourService;
module.sharedInjector();
beforeAll(module('app'));
beforeEach(inject(function(_yourService_) {
yourService = _yourService_;
}));
Hope this might help others as it took me almost a week to find out that this was the root cause of the issue and not Jasmine itself like some people think on github.

How can I navigate to failed Karma tests in WebStorm?

I'm able to run my Karma test suite from within WebStorm, using a Karma Run Configuration. After the tests run, the failed ones are shown in a tree on the left side. I would expect that I could click/double-click on a test name and be taken to the source code for the test, but that doesn't happen.
I'm pretty sure I've seen this work in a video before. How can I turn it on?
I looked at the documentation here but it wasn't very helpful: http://www.jetbrains.com/webstorm/webhelp/test-runner-tab.html
In Karma tool window click gear icon and check Auto scroll to source.

Sinon.stub works in karma, mocha with chrome running but not headless

I am working on an angularjs project where if a user scrolls on an element, the element calls scrollTop() to determine if another method should be called.
I wrote this sinon.stub
scrollTopStub = sinon.stub($.fn, "scrollTop").returns(50);
This is the validation
expect($.fn.scrollTop.calledOnce).to.be.true;
The tests pass when I run them using karma, mocha and chrome.
However, when I run the tests headless the sinon stub is never called.
Any thoughts?
I know this is an old question. But if someone bumps into this: this is probably a completely different issue. I run into a similiar one once almost been driven crazy by cryptic and misleading errors when running with phantomjs (I assume this is the headless browser) - in the end I found out that Phantomjs doesn't support bind (and some other es5 methods So code using them would fail. just adding es5 shim to the test index.html (or karma.conf or.. ) file solved the problem. Since I used sinon.js with phantomjs in another project I know this can work correctly

Resources