Search Posts

Category: unit-testing

How to test AngularJS controller using Karma-Jasmine

I am trying to use Jasmine and karma to set up unit tests like explained here, in my already developed Angularjs application. I created a simple test file for my controller like this: describe(‘controller test’, function() { beforeEach(module(‘ipaApp’)); var $controller, $rootScope; beforeEach(inject(function(_$controller_, _$rootScope_) { // The injector unwraps the underscores (_) from around the parameter // names when matching $controller = _$controller_; $rootScope = _$rootScope_; })); describe(‘$scope.grade’, function() { it(‘sets the strength to “strong” if […]

Jasmine Spy : Getting an error in returnValue

I am trying to write unit test case with jasmine spy. Below is my service code. function reset(someParam) { var deferred = $q.defer(); svcTypes.getTasksWithRoles().then(function (types) { if (types.HadError) return; // do some stuff.. deferred.resolve(results); }, function (errorResponse) { deferred.reject(errorResponse); }); return deferred.promise; } Below is my unit test case : describe(‘tests for svcWorkordertypes’, function () { beforeEach(angular.mock.module(‘workorders.service’)); beforeEach(angular.mock.module(‘workordertypes.service’)); var svcWorkordertypes; var svcTypes; var $q, $httpBackend; beforeEach(angular.mock.inject(function (_svcWorkordertypes_, _svcTypes_, _$q_, _$httpBackend_) { svcWorkordertypes = _svcWorkordertypes_; svcTypes […]

Jest and hybrid Angular/AngularJS application

I’m considering to use jest for testing my hybrid application (Angular + AngularJS). I’m using jest-preset-angular for Angular testing but I also need angular-mocks for AngularJS testing. The problem is when I import both jest-preset-angular and angular-mocks in setupJest.ts, all the tests (Angular and AngularJS) fail with Cannot read property ‘$injector’ of null error (coming from angular-mocks). If I import just jest-preset-angular all the Angular tests pass, if I import just angular-mocks all the AngularJS […]

TS2322 – Typescript compilation error with ng.IPromise

When using Angular 1.x in combination with Typescript, I’m facing some problems. Considering this code: get(id): ng.IPromise<Server.MyItem> { return this.$http.get(`${this.baseAddress}/${id}`).then(d =>; } It’s compiled by tsc, but I ‘m getting the following error: Type ‘IPromise<{}>’ is not assignable to type ‘IPromise’ I tried to use the more specific IHttpPromise, but it does not do the tricks.. Any idea? Source: AngularJS

Unit test ngRedux components? (Inject a real service in an angular mock object?)

We use Angular 1.5 and ngRedux. I’d like to write unit tests similar to these: Basically, the idea is: use something like ngtemplate-loader to load the HTML partials into webpack. then in the karma tests, you can $compile the component So far everything is good (i.e., the components load fine in karma) except that components need a fully configured ngRedux. So how can I configure this service to use in tests? (I know how […]

stubbing `window.location.reload` for unit tests – "Some of your tests did a full page reload!"

I have this in a controller: $ = function(){ return RolesService.remove({ data: role }) .then(function (v) { if (!(v && v.cdtError)) { $window.location.reload(); } }); } $window is injected into the controller and then in my unit test: let $window = {location: {reload: sinon.spy()}}; module(function ($provide) { $provide.value(‘$window’, $window); $window.onbeforeunload = sinon.spy(); }); the above stub doesn’t seem to do anything, because I still get this error: Some of your tests did a full page […]

How to implement a unit test for Angular JS controller with ajax service?

I have tried to write unit test for below code, but I can’t able to achieve it. Service code: angular.module(‘ActivityApp’).service(‘PersonService’, [ ‘$http’, function ($http) { var person = function () { return $http({ method: ‘GET’, url: ‘/apis/person’ }); }; return { Person: person } }]); Controller code angular.module(‘ActivityApp’).controller(‘PersonController’, [ ‘$scope’, ‘PersonService’, function ($scope, PersonService) { ‘use strict’; $scope.person = PersonService.Person().then(function(res) { alert( }); $ = function () { PersonService.Person().then(function (res) { alert( }); }; }]); […]

Angular unit test with karma jasmine and browserify

I am using this repo code to apply browserify in app. I want to write unit test for my app for this I include karma and karma browserify. My folder structure is : karma.config.js module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: ”, // frameworks to use // available frameworks: frameworks: [‘jasmine’ , ‘browserify’], // list of files / patterns to load […]

problems while doing unit testing with controller in angularjs

All the karma configuration are right and it runs fine with other controller files.But it could not run this controller file. I will be thankful if you guys find any solutions for this. 🙂 the screenshot of the source code, testing, and commond prompt are as follows 1. controller file 2. jasmine unit testing file 3. after running karma, error displays command prompt Source: AngularJS

angular unit test cannot load html template

Jhipster project(angular 4) blocked in unit test. below is my compontent.ts import { Component, OnInit, Input } from ‘@angular/core’; import { Observable } from ‘rxjs/Observable’; import { ReportListService } from ‘./report-list.service’; import ‘rxjs/add/observable/timer’; @Component({ selector: ‘gffr-report-list’, templateUrl: ‘./report-list.component.html’, styles: [require(‘./report-list.component.css’)] }) export class ReportListComponent implements OnInit { reportNames; constructor( private reportListService: ReportListService) { } ngOnInit(): void { const timer = Observable.timer(2000, 1000); timer.subscribe(t => { this.reportListService.checkReportFolder().subscribe((data) => { if (data.status === 200) { this.reportNames = […]

Next Page »