angularjestjsag-gridag-grid-angularangular-testing-library

AG-Grid gridReady not being called on tests with Angular and Jest


The gridReady method of the Angular AG Grid is not being called in the Jest tests. Do you know why?

Here you have my test code:

import {TestBed} from "@angular/core/testing";
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing";
import {AppComponent} from "./app.component";
import {FormsModule} from "@angular/forms";
import {AgGridModule} from "ag-grid-angular";

describe('AG Grid', () => {
  it('calls grid ready', async()=> {
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(BrowserDynamicTestingModule,
      platformBrowserDynamicTesting());

    await TestBed.configureTestingModule({
      imports: [
        FormsModule,
        AgGridModule.withComponents([]),
      ],
      declarations: [AppComponent]
    }).compileComponents();

    const fixture = TestBed.createComponent(AppComponent);
    const component = fixture.componentInstance;

    fixture.detectChanges();
    expect(component.api).toBeDefined();
    expect(component.columnApi).toBeDefined();

  });
})

Here you can see a code sandbox for it. Here is a GitHub repository.


Solution

  • The problem is solved using await fixture.whenStable() after fixture.detectChanges()