Testing Angular — Angular

Testing Angular menggunakan TestBed dan Jasmine/Jest. Test Komponen describe("CounterComponent", () => { let component: CounterComponent let fixture: ComponentF

Testing Angular menggunakan TestBed dan Jasmine/Jest.

Test Komponen

describe("CounterComponent", () => {
  let component: CounterComponent
  let fixture: ComponentFixture<CounterComponent>

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [CounterComponent],
    }).compileComponents()

    fixture = TestBed.createComponent(CounterComponent)
    component = fixture.componentInstance
    fixture.detectChanges()
  })

  it("should display initial count", () => {
    const el = fixture.nativeElement
    expect(el.querySelector("p").textContent).toContain("Count: 0")
  })

  it("should increment on click", () => {
    const button = fixture.nativeElement.querySelector("button")
    button.click()
    fixture.detectChanges()
    expect(fixture.nativeElement.querySelector("p").textContent)
      .toContain("Count: 1")
  })
})

Yang akan kamu pelajari