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
2
votes
0 answers

mock Observable concatMap function inside Unit test (Angular 5)

Edit: I figured it out, the problem is spyOn will make the function it spy on to return undefined, need to call and.callThough explicitly. I'm trying to test my component, which uses ngrx store. I'm stubbing my store in that unit test like…
1
vote
1 answer

Angular + Cypress component tests: Retrieving service via InjectionToken

I am writing some component tests for my Nx Angular application using Cypress. I am trying to retrieve an instance of a provider that I set up in my test: export const DASHBOARD_TOKEN = new InjectionToken('Dashbaord'); let data:…
afriedman111
  • 1,925
  • 4
  • 25
  • 42
1
vote
1 answer

NgxsFeatureModule does not have a module def (ɵmod property)

Running NX mono repo and Angular "@nrwl/angular": "15.2.4", "@angular/core": "~15.0.0", "@ngxs/store": "^3.7.6", "jest": "28.1.1", "@nrwl/jest": "15.2.4", The exciting part is, the component I am testing has nothing to do with Ngxs and spec file is…
HADI
  • 2,829
  • 1
  • 26
  • 26
1
vote
1 answer

Unit testing Angular interceptor without Testbed using Jest. How to check headers were appended with bearer token. [Subscription returns nothing]

So I'm attempting to unit test my interceptor with out using testbed. The interceptor merely appends the Auth headers with a bearer token. I can't seem to figure out how to actually check that those headers have been appended. When subscribe to the…
1
vote
0 answers

Angular Mock Router URL

I'm having a hard time trying to mock Router in my Jest Unit Test. I have a service breadcrumb.service.ts that takes Router as a dependency. It basically takes the URL from it and changes www.yoursite.com/section-one/page-one to ['home',…
Scottish Smile
  • 455
  • 7
  • 22
1
vote
1 answer

Unit test for renderer listen method on custom event

I have the below method that listens to a custom event and sets the focus of the element. I want to write a unit test for this method. I am trying to mock the focus element focus and using fakeAsync to provide a little delay and also mocking the…
anonymous
  • 1,499
  • 2
  • 18
  • 39
1
vote
0 answers

How do I unit test if an element is visible when the element is inside a aync *ngIf directive

I want to test if a div is visible in my Angular unit test. The portion of the HTML code I wanted to test is:
...some more html code....
Here hasId is a Promise boolean…
1
vote
1 answer

spyOn doesn't seem to work in angular unit test, actual code invoked

I have following test. It fails and appears that the spyOn is not mocking the service correctly or may be I don't understand it. I put a console in the component method and could see it when test runs, shouldn't the actual method be not called when…
a p
  • 1,121
  • 6
  • 26
  • 51
1
vote
0 answers

Cannot read properties of undefined (reading pipe) - Angular Jasmine testing

I have a service service1. I want to test the method getDetails() of this. This method uses another service service2 to make an HTTP call using getData() method and returns a promise. The function is as below: service1 { constructor( …
1
vote
1 answer

Angular unit tesing HTTP interceptor that appends XCSRF token in header

I am unit testing global HTTP interceptor that gets the XSCRF token from a service call getReponse() and appends it to the header only in POSTS request postResponse() call ( service call mentioned below ) So far I tried testing it but i am not able…
1
vote
1 answer

Unit testing in Angular - property value not updating

I have simple function which shows and hides dropdown menu on click event. Here is HTML code
Show/Hide menu
johannesMatevosyan
  • 1,974
  • 2
  • 30
  • 40
1
vote
2 answers

Unit Testing Specific Part Of Application

I am trying to test a part of our application. I know I can limit the tests by using fit, fdescribe, and changing the require.context in the test.ts file. However, I would like a way to test a specific folder, and all files under it, plus a couple…
kent
  • 21
  • 4
1
vote
1 answer

Angular Unit Test should create FAILED TypeError: Cannot read properties of undefined (reading 'preferences')

I am trying to create cover all the line (Jasmine / Karma) but I am getting the error as Cannot read property 'preferences' of undefined Here is my code for component code. export class PreferComponent implements OnInit { @Output()…
Swapnil Yeole
  • 406
  • 3
  • 12
  • 26
1
vote
1 answer

How to unit test "throw new Error()" clause in Jasmine

I have a component which makes use of a service. I customized what happens when the service throws an error by catching the error and throwing a new one with custom message. I've managed to come up with the below UT, but there are 2 problems with…
morrigannn
  • 69
  • 1
  • 7
1
vote
1 answer

mat-slide-toggle unit test case is showing error

I want to write test cases for mat slide toggle. Test case it('should call change method on slide change', async () => { const nativeElement = fixture.nativeElement; const slider = nativeElement.querySelector('.slider'); …