Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 "Failed: Cannot read property 'replace' of undefined" when running ng test

Tags:

I have checked the github issues for this issue and looked around on various stack overflow questions and blogs and stuff, and I can't find a solution to why I get this error when running "ng test" in my angular 2 app.

Here is what a test looks like:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { RouterModule } from "@angular/router";
import { Location,LocationStrategy } from "@angular/common";

describe('App: MemberAdmin', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports:[
        RouterModule
      ],
      providers:[
        Location,
        LocationStrategy
      ]
    });
  });

  it('should create the app', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app.loggedIn).toEqual(false);
  }));

});

My package.json looks like this:

{
  "name": "member-admin",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "postinstall": "typings install",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.4.10",
    "@angular/compiler": "^2.3.1",
    "@angular/compiler-cli": "^2.3.1",
    "@angular/core": "^2.4.10",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.4.10",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "~3.1.0",
    "angular-calendar": "^0.4.0",
    "angular2-datatable": "^0.5.2",
    "angular2-google-maps": "^0.16.0",
    "bootstrap-datepicker": "^1.6.4",
    "bootstrap-timepicker": "^0.5.2",
    "core-js": "^2.4.1",
    "mydatepicker": "^1.7.7",
    "ng2-datepicker": "^1.8.2",
    "ng2-datetime": "^1.2.2",
    "ng2-facebook-sdk": "^2.1.1",
    "ng2-file-upload": "^1.1.4-2",
    "rxjs": "5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.8"
  },
  "devDependencies": {
    "@angular/cli": "^1.0.0",
    "@types/jasmine": "^2.2.30",
    "@types/jquery": "^2.0.34",
    "@types/node": "^6.0.42",
    "awesome-typescript-loader": "2.2.1",
    "codelyzer": "1.0.0-beta.1",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.2.1",
    "typings": "1.0.4",
    "webdriver-manager": "10.2.5"
  }
}

This is the output when running ng test:

ng test 05 04 2017 18:19:47.574:WARN [karma]: No captured browser, open http://localhost:9876/ 05 04 2017 18:19:47.606:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/ 05 04 2017 18:19:47.607:INFO [launcher]: Launching browser Chrome with unlimited concurrency 05 04 2017 18:19:47.614:INFO [launcher]: Starting browser Chrome 05 04 2017 18:19:49.239:INFO [Chrome 56.0.2924 (Mac OS X 10.10.5)]: Connected on socket /#XvtU7IkkctSlo1kYAAAA with id 74276965 Chrome 56.0.2924 (Mac OS X 10.10.5) App: MemberAdmin should create the app FAILED Failed: Cannot read property 'replace' of undefined TypeError: Cannot read property 'replace' of undefined at _stripIndexHtml (webpack:///~/@angular/common/src/location/location.js:224:0 <- src/test.ts:9952:15) at new Location (webpack:///~/@angular/common/src/location/location.js:41:0 <- src/test.ts:9769:54) at DynamicTestModuleInjector.get (/DynamicTestModule/module.ngfactory.js:125:61) at DynamicTestModuleInjector.getInternal (/DynamicTestModule/module.ngfactory.js:187:48) at DynamicTestModuleInjector.NgModuleInjector.get (webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25 <- src/test.ts:19158:44) at TestBed.get (webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0 <- src/test.ts:6078:51) at CompiledTemplate.proxyViewClass.AppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:152:0 <- src/test.ts:29850:45) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0 <- src/test.ts:30278:49) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) at CompiledTemplate.proxyViewClass.AppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:108:0 <- src/test.ts:29806:21) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0 <- src/test.ts:30262:52) at ComponentFactory.create (webpack:///~/@angular/core/src/linker/component_factory.js:202:0 <- src/test.ts:13033:25) at initComponent (webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0 <- src/test.ts:6117:53) at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:334:0 <- src/test.ts:37048:26) at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- src/test.ts:23684:39) Chrome 56.0.2924 (Mac OS X 10.10.5) App: MemberAdmin should have as title 'app works!' FAILED Failed: Cannot read property 'replace' of undefined TypeError: Cannot read property 'replace' of undefined at _stripIndexHtml (webpack:///~/@angular/common/src/location/location.js:224:0 <- src/test.ts:9952:15) at new Location (webpack:///~/@angular/common/src/location/location.js:41:0 <- src/test.ts:9769:54) at DynamicTestModuleInjector.get (/DynamicTestModule/module.ngfactory.js:125:61) at DynamicTestModuleInjector.getInternal (/DynamicTestModule/module.ngfactory.js:187:48) at DynamicTestModuleInjector.NgModuleInjector.get (webpack:///~/@angular/core/src/linker/ng_module_factory.js:139:25 <- src/test.ts:19158:44) at TestBed.get (webpack:///~/@angular/core/bundles/core-testing.umd.js:826:0 <- src/test.ts:6078:51) at CompiledTemplate.proxyViewClass.AppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:152:0 <- src/test.ts:29850:45) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (webpack:///~/@angular/core/src/linker/view.js:580:0 <- src/test.ts:30278:49) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/DynamicTestModule/AppComponent/host.ngfactory.js:15:63) at CompiledTemplate.proxyViewClass.AppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:108:0 <- src/test.ts:29806:21) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (webpack:///~/@angular/core/src/linker/view.js:564:0 <- src/test.ts:30262:52) at ComponentFactory.create (webpack:///~/@angular/core/src/linker/component_factory.js:202:0 <- src/test.ts:13033:25) at initComponent (webpack:///~/@angular/core/bundles/core-testing.umd.js:865:0 <- src/test.ts:6117:53) at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:334:0 <- src/test.ts:37048:26) at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- src/test.ts:23684:39) Chrome 56.0.2924 (Mac OS X 10.10.5): Executed 3 of 3 (2 FAILED) (0.638 secs / 0.469 secs) Chrome 56.0.2924 (Mac OS X 10.10.5) ERROR
Disconnectedundefined Chrome 56.0.2924 (Mac OS X 10.10.5): Executed 3 of 3 (2 FAILED) (0.638 secs / 0.469 secs)

like image 884
Dan Avatar asked Apr 05 '17 16:04

Dan


1 Answers

I ran into this as well. The RouterTestingModule worked for me since I was just testing the component and not routing.

Here is what the documentation says:

 * This module sets up the router to be used for testing.
 * It provides spy implementations of {@link Location}, {@link LocationStrategy},
 * and {@link NgModuleFactoryLoader}.

And here is how to add it:

import {RouterTestingModule} from '@angular/router/testing';

describe('MyComponent', () => {
...
  beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
          RouterTestingModule,
          ...

```

You should not provide the Location and LocationStrategy providers in these tests if you don't need to test routing and are only testing the component.

like image 196
Splaktar Avatar answered Sep 24 '22 10:09

Splaktar