0

I'm trying to set up a StackBlitz with jasmine tests. I managed to add a simple test, the problem is that by doing that, I broke my routing. As soon as I click on one of the buttons, I get an error:

Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login'

I believe that this has something to do with the main.ts file. Probably I should initialize my AppModule (or AppRoutingModule?) somewhere, since that's where the routes are defined. But I can't figure out how.

As you can see in main.ts I commented out the old code. But everytime I try to add pieces of it, I get this error:

Error in /turbo_modules/@angular/core@11.2.0/__ivy_ngcc__/bundles/core.umd.js (29742:19)
A platform with a different configuration has been created. Please destroy it first.

EDIT: I've fixed the first error as explained by Alif50 BUT the routing is still not working. Here is my updated StackBlitz.


EDIT2: as a reference, this is my original application before I added the tests. As you can see, I can navigate between two pages. Is there no way to keep this functionality once Karma is running? I seem to be able to set up only one component at a time, the one that has tests running for it (i.e. if I write a test for LoginComponent, then I will see that component INSTEAD of the HeaderComponent... I'd like my original app to run just like it did, and on top of that to have my tests running)

PLB
  • 881
  • 7
  • 20

1 Answers1

2

You need to actually register the routes if you are going to click on them in RouterTestingModule.

import { Component } from "@angular/core";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
import { RouterTestingModule } from "@angular/router/testing";

import { HeaderComponent } from "./header.component";

@Component({})
class DummyComponent {} // Create a dummy component for router config

describe("HeaderComponent", () => {
  let component: HeaderComponent;
  let fixture: ComponentFixture<HeaderComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [HeaderComponent],
      imports: [
        RouterTestingModule.withRoutes([
          { path: "login", component: DummyComponent }, 
          { path: "activities", component: DummyComponent } // register the routes
        ])
      ]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(HeaderComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  it("should work", () => { // this test should work now after doing the above
    const login = fixture.debugElement.query(By.css("button")).nativeElement;
    login.click();
    // ... your other expects
  });
});

AliF50
  • 16,947
  • 1
  • 21
  • 37
  • Hi - now it doesn't display the error but it still doesn't navigate. this is just affecting the "RouterTestingModule". How do I handle the real routes? (I've updated my post) thanks! – PLB Feb 15 '21 at 07:55
  • Your new link doesn't work it seems. But check out this answer on how to test RouterLink. https://stackoverflow.com/a/39579009/7365461 – AliF50 Feb 15 '21 at 16:25
  • Thanks. I fixed the link. Ok I see, but I'm not actually trying to set up routing. I'm interested in having StackBlitz WITH tests ON TOP of an app that has routing. I'm not even sure it's possible. If it is, I believe it has something to do with the config in main.ts I'm adding another EDIT – PLB Feb 15 '21 at 18:16