testing angular services: module is not defined - angularjs

I'm trying to include some tests on my example project and I downloaded the Jasmine standalone, pasted into my html files and then created the spec.html to call all sources and tests files. When I open my spec.html file to see the results on the browser, it didn't recognize the module() and inject() methods.
PS: I already included the angular-mocks.js
any ideas?

Try angular.mock.module() instead of module()
Check that you don't need to include angular-mocks.min.js instead of angular-mocks.js

Related

Minified file breaks angular

So I'm working on an older solution where we're on Angular 1.6. I decided to make it somewhat more up to date by adding using ES6. We're using grunt as a task runner so I introduced babel as a watch-task, where I add transpiled files to its own dir. After that I concatinate the files to one big file using grunt-contrib-concat, and then minify by using grunt-contrib-uglify. The concatinated file runs fine, but when minified a function from babel is hoisted and with this, breaks angular when I try to run my site.
This is added to the top of the .min.js file
"use strict";function _asyncToGenerator(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,n){return function o(r,i){try{var a=t[r](i),d=a.value}catch(e){return void n(e)}if(!a.done)return Promise.resolve(d).then(function(e){o("next",e)},function(e){o("throw",e)});e(d)}("next")})}}
In the concatinated file, this file is added at line 3700 and doesn't break execution. Anyone tips would be greatly appreciated
Edit: This is different from the problem described bellow as all angular controllers, directives, services etc are annotated correctly.

Is it possible to use Jasmine without Karma for testing Angular/Node based Nw.js apps?

I've read ton's of tutorials, but I must admit that this testing stuff is still very confusing to me. I have a Nw.js app which (of course) uses NodeJS and also Angular. I've installed the Jasmine test framework globally via npm and wrote an example test which starts with the following lines, and placed it in the spec sub-directory:
describe ( 'Test for my controller', function () {
beforeEach ( module ('module_under_test') );
... and so on ...
});
When running the test by typing jasmine on the cmd line (from the root folder of the app), I get the following error message:
TypeError: module is not a function
I know that I have to include the Angular library somehow. But where? In a normal browser application, it is included in the HTML <script> tag, but I don't have this possibility. I also know that I could write a HTML file, which shows the Jasmine result page after tests have finished, but I would prefer to start Jasmine on the cmd line.
First I thought about adding the angular library to the "helpers" entry in jasmine.json. But it didn't work. The documentation of this file is unfortunately very poor. In the Angular documentation and tutorials it is always mentioned to use Karma. But my understanding is that Karma is only useful for testing with browsers, since it spawns an own webserver. This does not make sense in my case.
Would be great if somebody could give me a hint, thanks!

ReferenceError when testing angular service with Karma

I have a simple Angular.js service. It makes use of piece of code called esprima.
Esprima is referenced in index.html like so:
...script src="https://unpkg.com/esprima#~3.1/dist/esprima.js"...
and in my code, it is referenced like so:
var syntax = esprima.parse(jsCode, {tokens: true});
According to Esprima documentation, the CDN reference in html makes it a global variable so esprima usage in my angular.js service should work -- which it does.
However, when trying to exercise my angularjs service code from karma test, I am getting ReferenceError for 'esprima'. I am guessing that Karma has no use for my html file and does not pull in the Esprima package because of that.
I dont know how to "import" Esprima package in such a way that it is seen when used from the browser AND can be seen from Karma. Sorry, I am new to this and drinking from a fire hose here. Thank you
Never mind. Karma.config.js file contains the "files" section where you can list any .js files you want to bring in.

How do you reference external libraries with Jasmine + Resharper

I can run Jasmine unit tests from the Resharper 8.0 unit test runner.
I have a problem where any Javascript references that are normally in the html page (ie in my case Ext-Js) then I can't use the Resharper test runner, as you don't seem to have access to the HTML page that Resharper uses. (I assume it's generated as I could not locate it on disk)
I was thinking if there is a way to call or load your external library references from the Javascript test file directly instead of via the html page, then I could get this to work. I've not found if that is possible with Javascript (or Ext-Js) yet.
It seems the way to go at the moment is hardcoding include statements as special comments in the suite file (called doc-comments references), e.g.:
// include external files like so:
/// <reference path="/path/to/external-file.js" />
// than write your testing suite as usual:
describe('a silly suite', function() {
it('should test something is happening', function() {
expect(something).toBe('happening');
});
});
See this thread on the ReSharper community, as the source of this recommendation.
I wrote 2 repos for dealing with ExtJS 4 for unit testing using Karma test runner/ Jasmine 1.x and 2.0 versions and dealing with Async Issues: here they are: https://github.com/cgauthier/karma_jasmine_1_extjs4 and https://github.com/cgauthier/karma_jasmine_2_extjs4. The trick to loading external references is to add them as files in your modules declaration.

Loading mocks into an AngularJS unit test

I'm trying to setup my AngularJS application to test out controllers, routes, templates and so on, but I'm having an issue getting some of the helper methods provided by the angular-mocks.js to work (namely module and inject).
I'm using testacular to load up the test suite with the following files added before the specs:
files = [
MOCHA,
MOCHA_ADAPTER,
'../application/lib/angular.min.js',
'./lib/angular/angular-mocks.js',
'./lib/angular/angular-scenario.js',
'../application/application.js',
'./lib/chai.js',
'./lib/chai-should.js',
'./lib/chai-expect.js',
'./spec/**/*.js'
];
So far so good, but when I run the tests I get this issue:
ReferenceError: Can't find variable: module
Not sure where this is loaded. Am I missing something?
First thing to check is that all those files are getting loaded in the test browser. It's surprisingly easy to get a path wrong in your config and not realize it. If you're running testacular with autowatch, you can navigate to http://localhost:9876/context.html with a browser and use developer tools inspect elements/resources/network and see if anything is missing.
If everything is good there and you're still having problems, post some of your test code and I'll take a look.
UPDATE: It appears (strangely) from the comments in the source for angular-mocks.js (line 1635) that window.module is only available with Jasmine. It looks like you're using Mocha instead of Jasmine. This is very likely the culprit.
ANSWER:
I can't rightly take credit for this Matsko, since you figured it out yourself... but it turns out that the current AngularJS stable download and angular-seed contain an older version of ngMock that doesn't support Mocha. Manually replacing the mock file with the latest from the github repo solves the problem. Glad I could help ;-)
I ran into this issue today and I wanted to provide others with a complete summary of the required steps to get this working. First let's say you have a module named myApp. Inside that that module there is a service called myModel. the myModel service has a method named getItems().
Currently, the angular-mocks.js (I am using AngularJS 1.0.6) does not support Mocha. You will need to visit this link and replace the 1.0.6 version with the one in the master branch from the AngularJS GitHub project. An easy way to do this (if you have wget) is wget https://raw.github.com/angular/angular.js/master/src/ngMock/angular-mocks.js in the correct directory. If you use a properly configured Sublime or vim it can also easily pull it down for you.
Make sure your karma.conf.js file includes the angular-mocks.js file in files array
Somewhere in your tests.js file (maybe at the top level describe) include beforeEach(module('myApp')); or whatever you named your main module.
If you plan to use the service (or whatever you want to include in the test) in more than one place you can call another beforeEach where needed like this:
beforeEach(inject(function(myModel) {
mymodel = myModel;
}));
otherwise you just can inject where it is needed. Now the mymodel variable (notice this is the variable you assigned in the beforeEach above) will be available to you for testing in your next blocks. For example, you can now write:
describe('when fetched', function() {
it('should return 3 items', function() {
// console.log(mymodel.getItems());
expect(mymodel.getItems()).to.have.length(3);
});
});

Resources