karma-jasmine

Angular 2 Jasmine How to test a function of a component

浪尽此生 提交于 2019-12-01 05:12:56
How do I mock the click event of a function in Angular 2? For my Home Component I have: Home Component import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ moduleId: module.id, templateUrl: 'home.component.html', styleUrls: ['home.component.css'], selector: 'home', }) export class HomeComponent { constructor(private router: Router) { } redirectToUpload() { this.router.navigate(['/upload']); } redirectToAbout() { this.router.navigate(['/about']); } } Home Component spec import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import

karma start - passing parameters

血红的双手。 提交于 2019-12-01 04:45:16
Is there a way to pass a parameter thru the Karma command line and then read that somewhere in your tests? For instance, this is what want: karma start -branding="clientX" And then somewhere in my specs I would need to access this variable (I need the "clientX" value). Is this possible at all? It is possible to transmit parameters to test cases. It can be a bit tricky. What you can to do is check for __karma__.config.args in you test suite: it("get karma args", function () { console.log(__karma__.config.args); }); karma run If you want to pass arguments with karma run , then the above is all

Angular 4 unit testing with jasmine /karma with http post mocking - how to fix

六眼飞鱼酱① 提交于 2019-12-01 04:15:19
I have a service I want to unit test in angular 4 typescript jasmine. Now, the http is doing a post , and it returns an identity, however.. it is not sending anything. I want to just have good code coverage but i don't understand how to quite complete this mocking statement. here is the method for http post in my service file addSession() { let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post(this.url, JSON.stringify({}), options) .map((response: Response) => response.json()); } Then the SPEC FILE ,

Failed: Can't resolve all parameters for MatDialogRef: (?, ?, ?). unit testing Angular project

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:22:07
I am new to angular development and more new towards unit testing using jasmine. I have created a component to sow a dialog using angular material MatDialogRef, MAT_DIALOG_DATA from @angular/material. The component is working fine but the Unit testing is giving me an error which i am not able to resolve. I really need this to work and any help would be appreciated.... Thanks in advance..!!! Please find my code below: app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { HttpClientModule, HttpClient }

Angular2 material 'md-icon' is not a known element with Karma / Jasmine

不羁的心 提交于 2019-12-01 03:20:36
I'm working on an Angular2 application using @angular/material 2.0.0-alpha.11-3 angular-cli 1.0.0-beta.19-3 karma 1.2.0 karma-jasmine 1.0.2 Running it works fine but a couple of the tests where the template has a button with md-icon fail with template errors: ERROR: 'Unhandled Promise rejection:', 'Template parse errors: 'md-icon' is not a known element: 1. If 'md-icon' is an Angular component, then verify that it is part of this module. 2. If 'md-icon' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message My app.module.ts:

karma + Chrome not working after last auto-update of Chrome

戏子无情 提交于 2019-12-01 02:47:35
We use jenkins with a number of jenkins slave nodes running on Windows 7. Through jenkins we kick off karma tests to be run on the slave node to run our jasmine tests on the actual browsers. Suddenly last week after Chrome auto-updated to version "38.0.2125.104 m" this stopped working (karma unable to capture the browser). Here are our logs: [INFO] --- maven-karma-plugin:1.8:start (default-cli) @ module-translation --- [INFO] Executing Karma Test Suite ... [INFO] cmd /C karma start C:\dev\util\jenkinsMaster\workspace\ci_module_translation_browser\translation\target\test-classes\frontend

Disconnected (1 times), because no message in 10000 ms using Karma-Jasmine

爷,独闯天下 提交于 2019-12-01 02:32:19
Using Karma runner with jasmine. After all the configuration done, I type on terminal the below command: karma start public/javascripts/karma.conf.js But I am getting the below error where my browser is getting closed. INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome INFO [Chrome 34.0.1847 (Mac OS X 10.9.2)]: Connected on socket hDO3pMdVNGcBMDx4FI0w with id 60695552 WARN [Chrome 34.0.1847 (Mac OS X 10.9.2)]: Disconnected (1 times), because no message in 10000 ms. I changed the browserNoActivityTimeout in Karma config to 50 seconds

How to correctly use SharedModules with Router while testing angular 2 application?

泪湿孤枕 提交于 2019-12-01 02:12:18
问题 I'm trying to unit test a (angular 2) component with routerLink and routerLinkActive but all test cases fail with following error message. ( Note: I have stubbed the router and other related dependencies. Here's the reference that I used for the same.). Cannot read property 'subscribe' of undefined I have also noticed that when routerLink and routerLinkActive are removed from my template, all test cases will run without any errors. I think the error is caused by either RouterTestingModule or

Can't bind to 'matMenuTriggerFor' since it isn't a known property of 'button'

狂风中的少年 提交于 2019-12-01 02:07:21
I'm getting following error when I try to test an angular component: Error while running jest tests: Can't bind to 'matMenuTriggerFor' since it isn't a known property of 'button'. Here is my html: <button mat-button [matMenuTriggerFor]="menu">Menu</button> <mat-menu #menu="matMenu"> <button mat-menu-item>Item 1</button> <button mat-menu-item>Item 2</button> </mat-menu>` I'm using "@angular/material": "6.1.0", in my package.json. I've also imported all the required material dependencies in the beforeAll block under TestBed I also tried changing the property of the button from matMenuTriggerFor

Issue with angular.mock.inject method

微笑、不失礼 提交于 2019-12-01 02:03:48
I am using folloing jasmine test case 'use strict'; describe('companyService', function() { var $httpBackend, companyService; beforeEach(angular.mock.module('myApp')); beforeEach(angular.mock.inject(function(_$httpBackend_ , _companyService_) { $httpBackend = _$httpBackend_; companyService = _companyService_; })); it('should return a promise for getCompany function', function() { // expect(typeof companyService.getCompany('foobar').then).toBe('function'); }); }); i am getting the following error . as you can see above . i am not doing anything inside it block . minErr/<@C:/Users/userone