Testing Angular Components with Children using Jest
Fran Saoco

Fran Saoco @fransaoco

About: Software Engineer | Learning and Sharing

Location:
Spain
Joined:
Apr 14, 2025

Testing Angular Components with Children using Jest

Publish Date: Apr 17
1 1

parent-component.ts

@Component({
  selector: 'app-parent',
  template: `
    <div>
      <h1>Parent Component</h1>
      <app-child [value]="parentValue"></app-child>
    </div>
  `,
})
export class ParentComponent {
  parentValue = 'Parent Value';
}
Enter fullscreen mode Exit fullscreen mode

parent-component.spec.ts

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ParentComponent } from './parent.component';
import { Component, Input } from '@angular/core';
import { By } from '@angular/platform-browser';

// Child component mock
@Component({
  selector: 'app-child',
  template: '<div>{{ value }}</div>'
})
class MockChildComponent {
  @Input() value: string = '';
}

describe('ParentComponent', () => {
  let parentComponent: ParentComponent;
  let fixture: ComponentFixture<ParentComponent>;
  let childComponent: MockChildComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ParentComponent, MockChildComponent] // Use mock instead of real child
    });
    fixture = TestBed.createComponent(ParentComponent);
    parentComponent = fixture.componentInstance;
    childComponent = fixture.debugElement.query(By.directive(MockChildComponent)).componentInstance as MockChildComponent;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(parentComponent).toBeTruthy();
  });

  it('should pass the correct value to child component', () => {
    expect(childComponent.value).toBe('Parent Value');
  });

  it('should update child component when parent value changes', () => {
    parentComponent.parentValue = 'New Value';
    fixture.detectChanges();

    expect(childComponent.value).toEqual('New Value');
  });

});
Enter fullscreen mode Exit fullscreen mode

Tip: The mock child component (MockChildComponent) ensures you're only testing the parent component's behavior.

Comments 1 total

  • assetkad
    assetkadMay 3, 2025

    `import { ComponentFixture, TestBed } from '@angular/core/testing';

    import { ParentComponent } from './parent.component';
    import { By } from '@angular/platform-browser';
    import { Component, Input } from '@angular/core';

    @Component({
    selector: 'app-child',
    standalone: true,
    template: '

    {{ value }}',
    })
    class MockChildComponent {
    @Input() value: string = '';
    }

    describe('ParentComponent', () => {
    let parentComponent: ParentComponent;
    let fixture: ComponentFixture;
    let childComponent: MockChildComponent;

    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [ParentComponent],
    })
    .overrideComponent(ParentComponent, {
    set: {
    imports: [MockChildComponent],
    },
    })
    .compileComponents();

    fixture = TestBed.createComponent(ParentComponent);
    parentComponent = fixture.componentInstance;
    fixture.detectChanges();
    
    childComponent = fixture.debugElement.query(
      By.directive(MockChildComponent)
    ).componentInstance as MockChildComponent;
    fixture.detectChanges();
    
    Enter fullscreen mode Exit fullscreen mode

    });

    it('should create', () => {
    expect(parentComponent).toBeTruthy();
    });

    it('should pass the correct value to child component', () => {
    expect(childComponent.value).toBe('Parent Value');
    });

    it('should update child component when parent value changes', () => {
    parentComponent.parentValue = 'New Value';
    fixture.detectChanges();

    expect(childComponent.value).toEqual('New Value');
    
    Enter fullscreen mode Exit fullscreen mode

    });
    });
    `

Add comment