Angular: No provider for UntypedFormBuilder!

If you are getting the following error while running ng test:

NullInjectorError: R3InjectorError(DynamicTestModule)[UntypedFormBuilder -> UntypedFormBuilder]: NullInjectorError: No provider for UntypedFormBuilder!

the solution is to import ReactiveFormsModule in your spec file of your component


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

The full file will look like this:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';

import { YourComponent } from './YourComponent.component';

describe('YourComponent', () => {
  let component: YourComponent;
  let fixture: ComponentFixture<YourComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports:[ReactiveFormsModule],
      declarations:[....YourDeclarationsIfNeeded]
    }).compileComponents();

    fixture = TestBed.createComponent(YourComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});