Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Dependency injection in parent and child component in Angular

I was having issues with a test in angular . The strange thing was the tests succeeded with

ng test

and failed with

ng test --watch=false --browsers=ChromeHeadless --code-coverage

But adding constructor in the child component solved the issue.

Parent component :

 Component({
  templateUrl: './edit-organization.component.html',
  styleUrls: ['./edit-organization.component.scss']
})
export class EditOrganizationComponent implements OnInit {
  organization: Organization;
  errorReason: ErrorReason;
  organization$: Observable<Organization>;
  errorCodes: ErrorCode[];

  constructor(
    protected currentUserRoute: ActivatedRoute,
    protected organizationService: OrganizationService,
    protected navigationService: NavigationService
  ) { }

  ngOnInit(): void {
    this.organization = this.currentUserRoute.snapshot.data.organization;
  }

  updateOrganization(form: NgForm) { ...}

Child component 1 (The test used to fail here)

@Component({
  templateUrl: './edit-business-organization-profile.component.html',
})
export class EditBusinessOrganizationProfileComponent
  extends EditOrganizationComponent
  implements OnInit
{
  organizationServiceTypes: OrganizationServiceType[] =
    organizationServiceTypes;

  constructor(readonly currentUserRoute: ActivatedRoute,
              readonly organizationService: OrganizationService,
              readonly navigationService: NavigationService) {
    super(currentUserRoute, organizationService, navigationService);
  }
  ngOnInit(): void {
    super.ngOnInit();
  }
}

Child component 2 (The tests pass here)

@Component({
  templateUrl: './business-organization-profile.component.html',
})
export class BusinessOrganizationProfileComponent
  extends EditOrganizationComponent
  implements OnInit {
  ngOnInit(): void {
    super.ngOnInit();
  }
}

I have a almost similar test for both the component which were passing before but when I added a field organizationServiceTypes in EditBusinessOrganizationProfileComponent the tests started failing .

Earlier I didn't have any constructor in the component EditBusinessOrganizationProfileComponent, once I added the constructor the tests started getting passed.

Can someone explain how the dependency injection works in this case as the code works perfectly without the test , it is only the test that breaks ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!