Questions tagged [karma-jasmine]

Karma-Jasmine is an adapter for the Jasmine testing framework, which is shipped with Karma by default.

Karma-Jasmine is an adapter for the Jasmine testing framework.

Installation

This plugin ships with Karma by default, and there isn't any need to install it.

The easiest way is to keep karma-jasmine as a devDependency in your package.json file.

{
  "devDependencies": {
    "karma": "~0.10",
    "karma-jasmine": "~0.1"
  }
}

You can simply do it by:

npm install karma-jasmine --save-dev

Configuration

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],

    files: [
      '*.js'
    ]
  });
};
6563 questions
27
votes
2 answers

How to unit test if an angular 2 component contains another component

I'm quite new to angular 2. I have a component which in turn has some other components in its template. How do I write unit tests to check if my parent component consists of other components. Mentioning a sample or directing me to a resource would…
Chan15
  • 929
  • 2
  • 9
  • 16
27
votes
1 answer

Angular2 how to unit test a custom validator directive?

I wrote a very simple custom validator for an input field: import { Directive } from '@angular/core'; import { AbstractControl, NG_VALIDATORS } from '@angular/forms'; function numberValidator(c: AbstractControl) { if (!c.value) return null; …
26
votes
2 answers

Unit test Angular Material Dialog - How to include MAT_DIALOG_DATA

I am trying to unit test this material dialog to test if the template is rendering the right injected object. The component works fine when used properly Component - The Dialog export class ConfirmationDialogComponent { …
26
votes
1 answer

Jasmine: How to mock MutationObserver?

I have a angularjs component, HTML template
...
JS var myController = function() { var ctrl=this; ctrl.$onchanges = function() { } var…
user4235401
26
votes
6 answers

Testing - Can't resolve all parameters for (ClassName)

Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService's constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService,…
26
votes
2 answers

jasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())

I have a following controller: .controller('ProjectUserAddCtrl', ['$scope', 'ProjectUser', '$q', 'i18nNotifications', function($scope, ProjectUser, $q, i18nNotifications) { var buildUnassignedUsers = function(users, project) { var…
h3ndr1ks
  • 517
  • 2
  • 7
  • 18
25
votes
2 answers

Angular 11 Unit Test Code Coverage is Now Breaking

Prior to upgrading to Angular 11, I ran my unit tests with code coverage via the following command: ng test --project my-app --code-coverage true When I upgraded my project to Angular 11, I was still able to do my code coverage tests, but I started…
25
votes
6 answers

NullInjectorError: No provider for InjectionToken MatDialogData

I'm getting this error when running jasmine tests on my Angular app. Error: StaticInjectorError(DynamicTestModule)[MyEditDialogComponent -> InjectionToken MatDialogData]: StaticInjectorError(Platform: core)[MyEditDialogComponent -> InjectionToken…
Heinrich
  • 1,711
  • 5
  • 28
  • 61
25
votes
1 answer

Wait for ngOnInit to finish in Jasmine Angular2

I'm unit testing a component and this is what I have: describe('Component: nav', () => { beforeEach(() => addProviders([ provide(UserService, {useClass: MockUserService}), NavComponent ])); it('should have a property \'firstName\'…
stijn.aerts
  • 6,026
  • 5
  • 30
  • 46
25
votes
1 answer

Karma testing: TypeError: Attempted to assign to readonly property

When I am trying to unit test my controller I am getting the error.When I debug the testcase's expect I am getting the expected value but its failing with below error.What am I missing here or whether I am testing the controller variable in a wrong…
Raphael
  • 1,738
  • 2
  • 27
  • 47
25
votes
3 answers

Is jasmine supposed to execute specs in the order they are declared or in a random order?

un-comment the last spec. All hell breaks loose... why? describe('test', function() { var index = 1; it('test 1', function() { expect(index).toBe(1); index++; }); it('test 2', function() { expect(index).toBe(2); index++; …
Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
25
votes
3 answers

Unit testing a modalInstance controller with Karma / Jasmine

EDIT : Quick & Dirty solution at the end of this post I am using a modal window from AngularUI-Bootstrap in the same way that it is explained on the website, except that I splitted files. Therefore I have : CallingController.js : $scope.delete =…
24
votes
3 answers

No value accessor for form control with name... for mat-select controls

I am making some unit test with jasmine and karma for an angular 6 app that validate if a formGroup field is valid. I am experiencing problems with mat-select control. when I run the test case, Karma fires me an error saying Error: No value accessor…
xzegga
  • 3,051
  • 3
  • 25
  • 45
24
votes
1 answer

Angular 4 Error: No provider for ChildrenOutletContexts in Karma-Jasmine Test

My Angular application is working properly, but I am keep getting Karma error when I run ng test command. I have attached app component, spec, module and html along with package.json file. Error looks like this: Failed: No provider for…
bradd
  • 1,057
  • 2
  • 10
  • 22
24
votes
1 answer

Do I need to unsubscribe from subscriptions in my unit tests?

If I have a test such as the following: it('should return some observable', async(() => { mockBackend.connections.subscribe((mockConnection: MockConnection) => { const responseOptions = new ResponseOptions({ body: JSON.stringify(/* some…
user3244479