0

I don't know why I get the error of "TypeError: Cannot read property 'form' of undefined " I've tried every way to understand this error, but I can't I tryng test new setup for spec.ts, but now return error "Cannot read property 'form' of undefined" :
spec.ts

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CustomValidationService } from 'src/app/core/services/custom-validation/custom-validation.service';
import { UserService } from 'src/app/core/services/user/user.service';
import {FormsModule} from '@angular/forms';
import { AccountComponent } from './account.component';

describe('AccountComponent', () => {
  let accountComponent: AccountComponent;
  let fb:FormBuilder;
  let customValidator: CustomValidationService;
  let userService: UserService;
  let fixture: ComponentFixture<AccountComponent>;
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [AccountComponent],
      imports: [FormBuilder, CustomValidationService, UserService],
      providers:[FormBuilder]
    }).compileComponents();
  }))
 
  it('',fakeAsync(()=>{
    const testForm={
      username:'test',
      email: '[email protected]'
    };
    accountComponent.form.controls['username'].setValue(testForm.username)

  }))
});

My component:

  form: FormGroup;
  formAlterPassword: FormGroup;  
  constructor(private fb: FormBuilder) { 
    this.form = this.fb.group({
      username: ['', Validators.required],
      email: ['', Validators.required]
    });
    this.formAlterPassword = this.fb.group({
      currentPassword: ['', Validators.required],
      password: ['', Validators.compose([Validators.required, Validators.minLength(12)])],
      confirmPassword: ['', Validators.compose([Validators.required, Validators.minLength(12)])]      
    })
  }```

1 Answer 1

0

Your component relies on dependency injection to get an instance of FormBuilder, so you have to provide an instance to your test as well. Try something like this,

 let fixture: ComponentFixture<AccountComponent>;
 beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [ReactiveFormsModule],
        schemas: [NO_ERRORS_SCHEMA]
    });
    fb = TestBed.inject<FormBuilder>(FormBuilder);
    fixture = TestBed.createComponent(AccountComponent);
    accountComponent = fixture.componentInstance;
    fixture.detectChanges();
});

Alternatively take a look at the answers here for some other ways you can provide the FormBuilder,

Angular2 FormBuilder unit testing

You also have to create an instance of the AccountComponent before you can access it's properties. I added that code to my original answer as well.

Sign up to request clarification or add additional context in comments.

9 Comments

In this example you put in the link, I've been following it and it's returned an error: TypeError: Cannot read property 'form' of undefined
Can you add where/how you setup the provider to your code above?
I post a new setup
now error: Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
Remove FormBuilder from the imports in your code above
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.