import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { MyComponent } from './my.component'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ MyComponent ] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; }); it('should create', () => { expect(component).toBeTruthy(); }); });
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MyComponent } from './my.component'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixtureIn this example, we're using `querySelector` to grab elements from the component's template and make expectations about their behavior. We also use `fixture.detectChanges()` to detect changes in the template so that we can ensure that our expectations are correct. Both of these examples come from the `@angular/core/testing` package, which is provided by Angular itself.; beforeEach(() => { TestBed.configureTestingModule({ declarations: [ MyComponent ] }); fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should display a title', () => { const titleElement = fixture.nativeElement.querySelector('h1'); expect(titleElement.textContent).toContain('My Title'); }); });