title: Angular unit test case- Cannot read properties of undefined (reading 'forEachNode')
body: <p>I am trying to write unit test cases for Grid, for this I need to loop through gridapi data but it is giving me Cannot read undefined forEachNode error.</p>
  <p>myComponent.component.ts.</p>
  <pre><code>          import { Component, OnInit, } from '@angular/core';
            import { GridOptions, GridApi, } from &quot;ag-grid-community&quot;;
  
            @Component({
                  selector: 'my-component',
                  templateUrl: './mycomponent.component.html',
                  styleUrls: ['./mycomponent.component.scss']
            })
            export class MyComponent implements OnInit {
  
               grid: GridOptions;
               data;
               constructor() {}
               getData() {
                  this.grid.api.forEachNode((node) =&gt;
                     this.data.push(node.data.id);
                  );
               }   
            }
  </code></pre>
  <p>mycomponent.component.spec.ts:</p>
  <pre><code>          import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
            import { Mycomponent} from './mycomponent.component';
            
            describe('Mycomponent', () =&gt; {
                      let component: Mycomponent;
                      let fixture: ComponentFixture&lt;Mycomponent&gt;;
  
            beforeEach(
               waitForAsync(() =&gt; {
                 TestBed.configureTestingModule({
                   declarations: [Mycomponent],
                   }).compileComponents();
                  })
             );
  
            beforeEach(() =&gt; {
                fixture = TestBed.createComponent(Mycomponent);
                component = fixture.componentInstance;
                const gridObj = jasmine.createSpyObj('grid', [
                      'api',
                ]);
                gridObj.grid.api.and.returnValue({
                     context: {}
                     data : { id:1,name: 'columName'}
               });
               component.grid = gridObj;
               fixture.detectChanges();
            });
  
             it(`should call getData method.`, () =&gt; {
              component.getData();
              expect(component.data).toEqual('[1]');
             });
            });
  </code></pre>
  <p>When I try to run above test case,
  TypeError: Cannot read properties of undefined (reading 'forEachNode').
  Can any one tell me where I am doing wrong.</p>