Este es el archivo de prueba.
describe('test', () => { let component: myComponent; let fixture: ComponentFixture<myComponent> let loader: HarnessLoader; beforeEach(() => { fixture = TestBed.createComponent(myComponent); component = fixture.componentInstance; fixture.detectChanges(); loader = TestbedHarnessEnvironment.loader(fixture); }); it('check label', async () => { const form = await loader.getHarness(MatFormFieldHarness); // error line const label = await form.hasLabel(); expect(label).toBeTrue(); }); });este es el html
<div class="units-input-container"> <mat-form-field> <mat-label>unit</mat-label> <input type="text" required /> </mat-form-field> </div>este es el error que me sale
Error: Uncaught (in promise): Error: Failed to find element matching one of the following queries: (MatFormFieldHarness with host element matching selector: ".mat-form-field") Error: Failed to find element matching one of the following queries:Debe incluir los módulos de material angular utilizados en su componente, consulte a continuación:
beforeEach(async () => { await TestBed.configureTestingModule({ imports: [ ReactiveFormsModule, MatFormFieldModule, MatInputModule, NoopAnimationsModule ], declarations: [AppComponent] }) .compileComponents(); }); NOTA: Asumo que está usando un formulario reactivo ( ReactiveFormsModule ) y que puede usar mat-input , por eso MatInputModule . Se requiere NoopAnimationsModule para que la prueba maneje las animaciones.
Para más detalles consulta la documentación (ver documentación ).
Creé un ejemplo simple para experimentar con el marco de arnés. Consulte la prueba 'mat-form-field debe detectar errores después de que se activen las validaciones' en github .