this is from official documentation https://angular.io/docs/ts/latest/guide/testing.html#!#component-fixture. So you can create new input object expectedHero and pass it to the component comp.hero = expectedHero

Also make sure to call fixture.detectChanges(); last, otherwise property will not be bound to component.

Working Example

// async beforeEach
beforeEach( async(() => {
    TestBed.configureTestingModule({
        declarations: [ DashboardHeroComponent ],
    })
    .compileComponents(); // compile template and css
}));

// synchronous beforeEach
beforeEach(() => {
    fixture = TestBed.createComponent(DashboardHeroComponent);
    comp    = fixture.componentInstance;
    heroEl  = fixture.debugElement.query(By.css('.hero')); // find hero element

    // pretend that it was wired to something that supplied a hero
    expectedHero = new Hero(42, 'Test Name');
    comp.hero = expectedHero;
    fixture.detectChanges(); // trigger initial data binding
});
Answer from Vazgen Manukyan on Stack Overflow
🌐
Testing-angular
testing-angular.com › testing-components
Testing Components – Testing Angular
We need to test that the counter handles the Input properly. For example, if we set startCount to 123, the rendered count needs to be 123 as well. If the Input is empty, the rendered count needs to be 0, the default value.
Top answer
1 of 4
79

this is from official documentation https://angular.io/docs/ts/latest/guide/testing.html#!#component-fixture. So you can create new input object expectedHero and pass it to the component comp.hero = expectedHero

Also make sure to call fixture.detectChanges(); last, otherwise property will not be bound to component.

Working Example

// async beforeEach
beforeEach( async(() => {
    TestBed.configureTestingModule({
        declarations: [ DashboardHeroComponent ],
    })
    .compileComponents(); // compile template and css
}));

// synchronous beforeEach
beforeEach(() => {
    fixture = TestBed.createComponent(DashboardHeroComponent);
    comp    = fixture.componentInstance;
    heroEl  = fixture.debugElement.query(By.css('.hero')); // find hero element

    // pretend that it was wired to something that supplied a hero
    expectedHero = new Hero(42, 'Test Name');
    comp.hero = expectedHero;
    fixture.detectChanges(); // trigger initial data binding
});
2 of 4
57

If you use TestBed.configureTestingModule to compile your test component, here's another approach. It's basically the same as the accepted answer, but may be more similar to how angular-cli generates the specs. FWIW.

import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';

describe('ProductThumbnail', () => {
  let component: ProductThumbnail;
  let fixture: ComponentFixture<TestComponentWrapper>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ 
        TestComponentWrapper,
        ProductThumbnail
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
    .compileComponents();

    fixture = TestBed.createComponent(TestComponentWrapper);
    component = fixture.debugElement.children[0].componentInstance;
    fixture.detectChanges();
  });

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

@Component({
  selector: 'test-component-wrapper',
  template: '<product-thumbnail [product]="product"></product-thumbnail>'
})
class TestComponentWrapper {
  product = new Product()
}
Discussions

unit testing angular component with @input - Stack Overflow
I have an angular component which has an @input attribute and processes this on the ngOnInit. Normally when unit testing an @input I just give it as component.inputproperty=value but in this case I cannot since its being used on the ngOnInit. How do I provide this input value in the .spec.ts file. More on stackoverflow.com
🌐 stackoverflow.com
unit testing angular component with input.required - Stack Overflow
const testedComponentInput = ... // other tests after changing value · As mentioned, new API has more methods, these are: outputBinding and twoWayBinding which allow you to set signals that will be bound to output or will handle two-way binding ... Sign up to request clarification or add additional context in comments. ... Not working for me, I get the following error: NG0303: Can't set value of the 'example' input on the 'ExampleComponent' component... More on stackoverflow.com
🌐 stackoverflow.com
angular - Testing an @input() of a component - Stack Overflow
I'm trying to test this input in my nav-bar component , but for some reason I receive null. The design of my code for the child and parent components would be something like this: &l... More on stackoverflow.com
🌐 stackoverflow.com
typescript - How to create component with @input on unit tests Angular? - Stack Overflow
I'm having problems with creating a component for testing that has an @input. Inside the component it makes use of properties that are in the model feedback, I did the import in the test file but it More on stackoverflow.com
🌐 stackoverflow.com
🌐
Angular
angular.dev › guide › testing › components-scenarios
Component testing scenarios • Angular
The testing goal is to verify that such bindings work as expected. The tests should set input values and listen for output events. The DashboardHero component is a tiny example ...
🌐
CodeCraft
codecraft.tv › courses › angular › unit-testing › components
Testing Components • Angular - CodeCraft
In the next lecture we will look at how to test Directives. Listing 1. login.component.ts · import { Component, EventEmitter, Input, Output } from '@angular/core'; export class User { constructor(public email: string, public password: string) { } } @Component({ selector: 'app-login', template: ` <form> <label>Email</label> <input type="email" #email> <label>Password</label> <input type="password" #password> <button type="button" (click)="login(email.value, password.value)" [disabled]="!enabled">Login </button> </form> ` }) export class LoginComponent { @Output() loggedIn = new EventEmitter<User>(); @Input() enabled = true; login(email, password) { console.log(`Login ${email} ${password}`); if (email && password) { console.log(`Emitting`); this.loggedIn.emit(new User(email, password)); } } }
🌐
Modern Angular
modernangular.com › articles › getting-started-with-unit-testing-in-angular
A great way to start testing in Angular is to focus on testing the inputs and outputs of your dumb components
You can use other types of selectors, like class names or tag names, but using a testid helps prevent your tests from breaking due to refactors - for example, you swap out an h2 tag for an h1 tag and your test breaks because the selector is wrong. After writing a test, it is important to see that it actually fails - but before we can do that I will need to set up this checklists input on the real component, as it doesn’t actually exist yet and it will prevent our test from compiling.
🌐
DEV Community
dev.to › fransaoco › testing-angular-components-with-inputs-and-outputs-using-jest-1ch6
Testing Angular Components with Inputs and Outputs using Jest - DEV Community
April 21, 2025 - Control input values passed to the component. Capture emitted events exactly like a real parent would. io-component.spec.ts · import { ComponentFixture, TestBed } from '@angular/core/testing'; import { IoComponent } from './io.component'; import { Component, ViewChild } from '@angular/core'; @Component({ template: ` <app-io [color]="hostColor" (colorChange)="onColorChanged($event)"> </app-io>` }) class HostComponent { @ViewChild(IoComponent) ioComponent!: IoComponent; hostColor = 'red'; emittedColor = ''; onColorChanged(color: string) { this.emittedColor = color; }; } describe('IoComponent',
Find elsewhere
🌐
Testim
testim.io › blog › angular-component-testing-detailed-guide
Angular Component Testing: A Detailed How-To With Examples
June 18, 2025 - Now that we know a few Angular component testing basics, let’s create a component where we eventually will write our own unit tests. First, we’ll create a TitleComponent. TitleComponent has a text input and a button used to change the title ...
🌐
Angular
angular.dev › guide › testing › components-basics
Basics of testing components • Angular
But in many cases, testing the component class alone, without DOM involvement, can validate much of the component's behavior in a straightforward, more obvious way. ... A component is more than just its class. A component interacts with the DOM and with other components. Classes alone cannot tell you if the component is going to render properly, respond to user input ...
🌐
Medium
medium.com › @simplycodesmart › inputsync-building-and-testing-responsive-inputs-with-signals-in-angular-a1ad04f042df
InputSync: Building and Testing Responsive Inputs with Signals in Angular | by Krishna Kanth | Medium
June 26, 2024 - import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ReceiverComponent } from './receiver.component'; describe('ReceiverComponent', () => { let component: ReceiverComponent; let fixture: ComponentFixture<ReceiverComponent>; beforeEach(async () => { await TestBed.configureTestingModule({ imports: [ReceiverComponent] }).compileComponents(); fixture = TestBed.createComponent(ReceiverComponent); component = fixture.componentInstance; }); it('should create', () => { expect(component).toBeTruthy(); }); it('should display the input data', () => { fixture.componentRef.setInput('
🌐
Angular
v17.angular.io › guide › testing-components-scenarios
Component testing scenarios
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
Top answer
1 of 1
21

Doing a test host component is a way to do it but I understand it can be too much work.

The ngOnInit of a component gets called on the first fixture.detectChanges() after TestBed.createComponent(...).

So to make sure it is populated in the ngOnInit, set it before the first fixture.detectChanges().

Example:

fixture = TestBed.createComponent(BannerComponent);
component = fixture.componentInstance;
component.inputproperty = value; // set the value here
fixture.detectChanges(); // first fixture.detectChanges call after createComponent will call ngOnInit

I assume all of that is in a beforeEach and if you want different values for inputproperty, you have to get creative with describes and beforeEach.

For instance:

describe('BannerComponent', () => {
  let component: BannerComponent;
  let fixture: ComponentFixture<BannerComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({declarations: [BannerComponent]}).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(BannerComponent);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    expect(component).toBeDefined();
  });

  describe('inputproperty is blahBlah', () => {
   beforeEach(() => {
     component.inputproperty = 'blahBlah';
     fixture.detectChanges();
   });

   it('should do xyz if inputProperty is blahBlah', () => {
     // test when inputproperty is blahBlah
   });
  });

  describe('inputproperty is abc', () => {
   beforeEach(() => {
     component.inputproperty = 'abc';
     fixture.detectChanges();
   });

   it('should do xyz if inputProperty is abc', () => {
     // test when inputproperty is abc
   });
  });
});
🌐
Medium
otchibozo.medium.com › angular-unit-testing-with-input-and-output-in-an-angular-component-df11296f9c9a
Angular Unit Testing with @Input() and @Output() in an Angular Component | by Olympe Tchibozo | Medium
February 16, 2024 - Angular Unit Testing with @Input() and @Output() in an Angular Component Understanding Angular Unit Testing: The Car Profiles Case Writing unit tests can often seem a complex task for many Angular …
Top answer
1 of 4
68

To set inputs, you can use ComponentRef<T>.setInput method, which also works with signals:

fixture = TestBed.createComponent(NewComponent);
component = fixture.componentInstance;
fixture.componentRef.setInput('signal-input-name', 'value'); 
// e.g. setInput('myInput', 'test');
fixture.detectChanges();

This method takes the input name as a first parameter and the value to set signal input to as second parameter.

More in angular.dev documentation


EDIT: New API added in Angular 20.1 (commit 2e0c98bd3f)

In Angular 20.1 new API was added to testing package - inputBinding function (and the same for outputs), that allows you to bind component inputs (and outputs) to specified value or other signal.

With static value:

const fixture = TestBed.createComponent(NewComponent, {
  bindings: [
    inputBinding('signal-input-name', () => 'value')
  ]
});
fixture.detectChanges();

With reactive value:

const testedComponentInput = signal('value');
const fixture = TestBed.createComponent(NewComponent, {
  bindings: [
    inputBinding('signal-input-name', testedComponentInput)
  ]
});
// some tests...
testedComponentInput.set('new value!');
// other tests after changing value

As mentioned, new API has more methods, these are: outputBinding and twoWayBinding which allow you to set signals that will be bound to output or will handle two-way binding

2 of 4
7

You can use signalSetFn

import { SIGNAL, signalSetFn } from '@angular/core/primitives/signals';


fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
signalSetFn(component.required[SIGNAL], 'test');
fixture.detectChanges();
🌐
Angular
v17.angular.io › guide › testing-components-basics
Basics of testing components
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
🌐
The Artisan
scotch83.github.io › 2024 › 07 › 04 › angular-standalone-components-unit-testing.html
Unit Testing in Angular Using Standalone Components and Jasmine
Before we dive into unit testing, let’s see how to create a standalone component with an injected service, signal properties, and input properties using the input function. Here’s a basic example: import { CommonModule } from '@angular/common'; import { Component, computed, input } from '@angular/core'; import { GreetingService } from './greeting.service'; @Component({ selector: 'app-hello-world', standalone: true, imports: [CommonModule], template: `<h1>{{ message() }}</h1>`, }) export class HelloWorldComponent { name = input<string>('World'); message = computed(() => this.greetingService.getGreeting() + ', ' + this.name() + '!'); /** * */ constructor( protected readonly greetingService: GreetingService ) { } }
🌐
Angular Minds
angularminds.com › blog › mastering-angular-component-testing-a-step-by-step-guide
Mastering Angular Component Testing: A Step-by-Step Guide
June 20, 2024 - Testing components that use these decorators involves ensuring that the input properties are correctly bound and that events emitted by the component are handled appropriately. Create one component that includes @input and @output decorator.