Sub topic 7
Testing inputs and outputs
This chapter assumes you can create a component with TestBed, call fixture.detectChanges(), and query the DOM. It uses the CounterComponent with a step signal input and a changed output.
Setting inputs
A signal input created with input() is read-only from outside the component. Assigning to it in a test fails to compile and does nothing useful. Use fixture.componentRef.setInput(name, value) instead. It is the same path Angular uses when a parent binds a value in a template.
- The name is the public input name. If the input has an alias, use the alias.
- It works for both signal inputs and
@Input()decorator inputs. - It marks the component as needing a check. The template does not update until you call
fixture.detectChanges(). - It throws if the component has no input with that name, so a misspelled name fails the test.
Listening to outputs
An output created with output() is an OutputEmitterRef. It has a subscribe method, so a test can attach a vi.fn() spy and then assert on the calls. Subscribe before you trigger the action that emits.
A complete test
This file tests the default input value and a custom one. Each test makes its own fixture, so state never leaks between tests.
import { TestBed } from '@angular/core/testing';
import { describe, it, expect, vi } from 'vitest';
import { CounterComponent } from './counter.component';
function setup(step?: number) {
const fixture = TestBed.createComponent(CounterComponent);
if (step !== undefined) {
fixture.componentRef.setInput('step', step);
}
const onChanged = vi.fn();
fixture.componentInstance.changed.subscribe(onChanged);
fixture.detectChanges();
const click = () => {
const button: HTMLButtonElement = fixture.nativeElement.querySelector('button');
button.click();
fixture.detectChanges();
};
const text = (): string =>
fixture.nativeElement.querySelector('[data-testid="count"]').textContent;
return { fixture, onChanged, click, text };
}
describe('CounterComponent inputs and outputs', () => {
it('adds 1 per click when step is not set', () => {
const { click, text, onChanged } = setup();
click();
click();
expect(text()).toContain('Count: 2');
expect(onChanged).toHaveBeenNthCalledWith(1, 1);
expect(onChanged).toHaveBeenNthCalledWith(2, 2);
});
it('uses a step that changes after the first render', () => {
const { fixture, click, text, onChanged } = setup(5);
click();
fixture.componentRef.setInput('step', 10);
click();
expect(text()).toContain('Count: 15');
expect(onChanged).toHaveBeenLastCalledWith(15);
});
});The setup helper keeps each test short. Only the behavior being checked appears in the test body.
Common errors
- Setting an input after
detectChangeswithout calling it again. The DOM keeps showing the old value, though the signal inside the component has changed. - Subscribing after the click. The spy never sees the emission, and
toHaveBeenCalledfails. - Reading
countdirectly. It isprotected, so the test cannot reach it. Assert on the rendered text instead, which is what a user sees.
Exercise
A test calls fixture.componentRef.setInput('step', 3) after the first fixture.detectChanges(), clicks the button, and then reads the text without a second fixture.detectChanges(). Will the displayed count reflect the click, and why?
Show answer
No. increment() updates the signal, but the template is only re-rendered when fixture.detectChanges() runs. The test must call detectChanges() after the click and before it reads the text.