Given this component ...
export class MyComponent { @Output() onActivateEditMode = new EventEmitter<boolean>(); constructor() {} emitActivateEditMode(flag: boolean) { this.onActivateEditMode.emit(flag); } ... and this template ...
<a (click)="emitActivateEditMode(true)" data-test-start-edit-project-btn>Edit</a> ... then this test fails:
describe('MyComponent', () => { ... (TestBed imports etc) beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; spyOn(component, 'emitActivateEditMode'); fixture.detectChanges(); }); it('should activate editmode on click', () => { const onActivateEditModeSpy = jest.spyOn( component.onActivateEditMode, 'emit' ); const startEditProjectBtn = fixture.debugElement.nativeElement.querySelector( '[data-test-start-edit-project-btn]' ); startEditProjectBtn.dispatchEvent(new Event('click')); // startEditProjectBtn.click() doesn't change the result, too fixture.detectChanges(); expect(component.onActivateEditMode.emit).toHaveBeenCalledWith(true); // expect(onActivateEditModeSpy).toHaveBeenCalledWith(true) ... doesn't change the result, too }); }); Test output is:
Expected: true Number of calls: 0 The functionality works in the browser, but something on this test setup is wrong.
@Outputemits, use e.g. angular.io/guide/testing#component-inside-a-test-host.