3

Here is my basic setting of the unit test:

  describe('ShellViewComponentAttributesComponent', () => {
  let fixture: ComponentFixture<ShellSelectLanguageForEEComponent>;
  let component: ShellSelectLanguageForEEComponent;
  let store: MockStore<any>;
  let location: Location;
  let router: Router;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ShellSelectLanguageForEEComponent],
      imports: [
        [
          RouterTestingModule.withRoutes([
            {
              path: 'SC/subject-curriculum',
              component: ShellSelectLanguageForEEComponent
            }
          ])
        ]
      ],
      providers: [
        provideMockStore({})
      ],

      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    }).compileComponents();

    router = TestBed.get(Router);
    location = TestBed.get(Location);

    fixture = TestBed.createComponent(ShellSelectLanguageForEEComponent);
    router.initialNavigation();
    component = fixture.componentInstance;

    store = TestBed.get(Store);
    spyOn(store, 'dispatch');
  }));

when i run the code, I am getting multiple error like:

console.warn node_modules/@angular/core/bundles/core.umd.js:25782
  Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?

I am not trigger any navigation. is it required? because i added in module already. how to handle this issue. I came across search but no solution works for me.

user2024080
  • 1
  • 14
  • 56
  • 96

1 Answers1

1

you have the answer in the following doc https://dev.to/this-is-angular/testing-angular-routing-components-with-the-routertestingmodule-4cj0

For your code wrap the code triggering routing in a Zone:enter code here

// @ts-ignore
fixture.ngZone.run(() => {
  router.initialNavigation();
});
Gauthier Peel
  • 1,438
  • 2
  • 17
  • 35