angularangular2-componentsangular2-testingangular2-cli

Location class from @angular/common doesn't work in unit tests


For some reason Location.normalize() and other functions doesn't return correct response when I run tests:

import { Router } from '@angular/router';
import { Location } from "@angular/common";
import { TestBed, async, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

describe('Location', () => {

  let location: Location;
  let router = {
    navigate: jasmine.createSpy('navigate')
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule(
      {
        declarations: [],
        providers: [
          {
            provide: Router,
            useValue: router
          }
        ],
        imports: [
          RouterTestingModule
        ]
      }
    ).compileComponents();

    location = TestBed.get(Location);
  }));
  it('TEST', async(() => {
    expect(location.normalize('/test/')).toBe('test');
  }));
});

Console log:

Chrome 61.0.3163 (Linux 0.0.0) Location TEST FAILED
    Expected null to be 'test'.
        at new ZoneAwareError home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:917:1)
        at Object.<anonymous> home/yuriy/projects/frontend/checkout/src/modules/checkout/services/finished-flow.guard.spec.ts:143:42)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:365:1)
        at AsyncTestZoneSpec.webpackJsonp.../../../../zone.js/dist/async-test.js.AsyncTestZoneSpec.onInvoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/async-test.js:49:1)
        at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/proxy.js:76:1)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:364:1)
        at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runGuarded home/yuriy/projects/frontend/checkout/node_modules/zone.js/dist/zone.js:138:1)
        at runInTestZone home/yuriy/projects/frontend/checkout/node_modules/@angular/core/bundles/core-testing.umd.js:110:1)
        at Object.<anonymous> home/yuriy/projects/frontend/checkout/node_modules/@angular/core/bundles/core-testing.umd.js:49:1)
Chrome 61.0.3163 (Linux 0.0.0): Executed 3 of 3 (1 FAILED) (0 secs / 0.385 secs)

Dependencies:

"@angular/cli": "1.3.0",
"@angular/common": "2.4.7",
"@angular/compiler": "2.4.7",
"@angular/compiler-cli": "2.4.7",
"@angular/core": "2.4.7",
"@angular/forms": "2.4.7",
"@angular/http": "2.4.7",
"@angular/platform-browser": "2.4.7",
"@angular/platform-browser-dynamic": "2.4.7",
"@angular/router": "3.4.7",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"zone.js": "0.8.5"

Why normilize returns null? Accourding documentation:

normalize(url: string): string;

Given a string representing a URL, returns the normalized URL path without leading or trailing slashes.


Solution

  • Because the RouterTestingModule provides a different implementation, SpyLocation, of the Location service. And this implementation always returns null.