Questions tagged [angular-unit-test]

Questions about Angular unit testing. Use this tag for Angular unit testing questions which are not specific to an individual version. Do not use this tag for the older AngularJS (1.x) web framework uni testing questions.

Angular unit testing is based on two systems. Jasmine (The testing framework) and Karma (The test runner).

The Angular CLI takes care of Jasmine and karma configuration for you.

You can fine-tune many options by editing the karma.conf.js and the test.ts files in the src/ folder.

The projects that you create with the CLI are immediately ready to test. Just run ng test CLI command to run the tests.

Useful Links

256 questions
3
votes
0 answers

Angular Material unittest component with datepicker

I'm trying to unittest a component which contains a Material Datepicker. I got the following error: Error: MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule,…
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
3
votes
0 answers

Gitlab CI get "[Launcher]: No binary for Chrome browser on your platform" for Angular app

I can serve my Angular project locally and as well get passed Karma and Jasmin report on http://localhost:9876 but during running test process in terminal, get this error: Please, set "CHROME_BIN" env variable. and my Gitlab CI as run pipeline…
3
votes
2 answers

Unit Test FormGroup Cannot Read Prop value of undefined

Trying to write a unit test where in my component, i update a value in a FormGroup in a FormArray. However, when running the test i am presented with the error: Cannot Read Prop value of undefined Running the app, the code compiles fine and the…
Oam Psy
  • 8,555
  • 32
  • 93
  • 157
3
votes
2 answers

Fake file drop event for unit testing

I'm trying to simulate a file Drop event for testing an angular directive but I cannot manage to create the DragEvent. it('should output file drop when file droped', () => { const file = new File([''], 'dummy.txt'); // got my file right there,…
Ced
  • 15,847
  • 14
  • 87
  • 146
3
votes
2 answers

ng-bullet vs karma-parallel vs configuring test module in before all - Angular unit test performance improvement

I am trying to figure out the best and reliable solution to speed up my unit tests written in angular project. I have gone through some couple of blogs which suggests to use either (1) ng-bullet (2) karma-paralle (3) ng test --browsers…
shreyansh
  • 1,637
  • 4
  • 26
  • 46
3
votes
1 answer

Angular Unit Test: How to unit test .map()?

I need to test one of services method and I'm not sure about how to achieve 100% code coverage for parts of code that run as result of calling some injected service method. Service Method to be tested: @Injectable() export class BomRevisiosnsService…
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
3
votes
0 answers

Angular unit test route stuck on root instead of route defined in redirectTo

I'm trying to unit test a directive but I'm having a strange issue regarding the routing setup. Despite having a redirect to foo for path: '' the route is still / when I log it inside the test case. So my question is why is the route not foo when I…
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
3
votes
0 answers

Angular 6 Unit Test: Can't bind to since it isn't a known property of & is not a known element

I have an angular application with multiple components and modules. Here, I have tried to simplify and minimize the structure to help understand the issue. App Structure -app -Core -donut-card -donut-card-component.html …
3
votes
1 answer

Angular Unit Testing - Mock async calls of injected services with TestBed

I need to write unit tests for the following DataService , @Injectable() export class DataService { constructor(private config: ConfigService, private http: HttpClient) { } ..... someMethod(){ let apiUrl =…
JPS
  • 2,730
  • 5
  • 32
  • 54
3
votes
1 answer

Angular unit test failing when using (submit)

Using Angular v4.4.4, I am saving a form using the (submit) event on the
element. On the live code everything works correctly. However, in the unit test clicking on a
ebakunin
  • 3,621
  • 7
  • 30
  • 49
2
votes
0 answers

Negative scenario creating conflict with positive angular unit testing

I've written the test case for services where I'm testing Negative and positive scenarios both but giving me errors for the negative scenario. I've written in order first I'm checking the negative scenario and then the positive one. Configuration…
2
votes
1 answer

How test function call inside ngOnChanges in angular jasmine?

I have a child component in angular app which calls function in ngOnChanges lifecycle hook. export class GameComponent implements OnInit, OnChanges { ngOnChanges(changes: SimpleChanges) { this.generateRandomIcon(); } …
johannesMatevosyan
  • 1,974
  • 2
  • 30
  • 40
2
votes
1 answer

Unit testing ngAfterViewInit

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'dataSource': 'undefined'. Current value: '[object Object]'. In my component.ts I am getting this error. due to having code in…
2
votes
1 answer

How to unit test MatSnackbar using Jasmine Karma in Angular

I want to perform unit testing on this onSubmit function where the positive submission is leading to MatSnackbar with message, "Submitted Successfully". I am new to Jasmine Karma please guide me about the testing of MatSnackbar in this…
user14076233
2
votes
3 answers

How can I cover rxjs subscribe method with unit tests?

I`m trying to write unit tests to cover every line of my code. I have two lines of my code that are not covered. I can`t understand where I make a mistake and how I can cover these lines of code? Here is an image of not covered lines of…
Nikolay
  • 59
  • 1
  • 8
1 2
3
17 18