Error: Invalid configuration of route '': Encountered undefined route.

Viewed 1439

trying Unit testing out in Angular 4 and getting the following error:

Error: 
          Invalid configuration of route '': Encountered undefined route.
          The reason might be an extra comma.
          Example:
          const routes: Routes = [
            { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
            { path: 'dashboard',  component: DashboardComponent },, << two commas
            { path: 'detail/:id', component: HeroDetailComponent }
          ];

however if add my routing paths in manually in RouterTestingModule.withRoutes(), it seems they work perfect so I'm concluding that my import of the routing from app-routing.module.ts is messing up somewhere?

app-routing.module.ts

import {Routes, RouterModule} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';
import {ScreenComponent} from './screen/screen.component';

const router: Routes = [
  {path: '', redirectTo: 'https://www.examplehere.com', pathMatch: 'full'},
  {path: 'home/:rmId/:leadId', component: ScreenComponent},
  {path: '**', redirectTo: 'https://www.examplehere.com'}
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router)

spec file

import { ScreenComponent } from './screen/screen.component';
import { async, getTestBed, inject, TestBed } from '@angular/core/testing';
import { DropdownModule } from 'primeng/primeng';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { Http } from '@angular/http';
import { AppGlobalService } from './shared/app.global.service';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { routes } from './app-routing.module';

describe('component: ScreenComponent', () => {
      let router, location, injector;

      beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [
            DropdownModule,
            ReactiveFormsModule,
            FormsModule,
            RouterTestingModule.withRoutes([
              // {path: 'home/:rmId/:leadId', component: ScreenComponent}
              router
            ])],
          declarations: [ ScreenComponent ],
          providers: [ {provide: AppGlobalService}, {provide: Http} ]
        });

        beforeEach(inject([Router, Location], (_router: Router, _location: Location) => {
          location = _location;
          router = _router;
        }));

        injector = getTestBed();
        location = injector.get(Location);
        router = injector.get(Router);

      });

      it('should go home', async(() => {
        const fixture = TestBed.createComponent(ScreenComponent);
        router.navigate(['home/:rmId/:leadId']).then(() => {
          expect(location.path()).toBe('/home/:rmId/:leadId');
        });
      }));

    });
0 Answers
Related