title: App testing return "NullInjectorError: No provider for Location!"
body: <p>Testing an app in Angular 7 with Karma, I can't remove the error in subj.</p>
  
  <p>I have searched various places (mostly here) but either the solutions don't work or are not relevant to my case.</p>
  
  <p>App.component.html:</p>
  
  <pre><code>&lt;app-header&gt;&lt;/app-header&gt;
  &lt;router-outlet&gt;&lt;/router-outlet&gt;
  </code></pre>
  
  <p>Header.component.ts:</p>
  
  <pre><code>import { Component, OnInit, ViewChild, ElementRef, AfterViewInit, EventEmitter, Output } from '@angular/core';
  import { Router } from '@angular/router';
  import { Location } from '@angular/common';
  
  @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.less']
  })
  export class HeaderComponent implements AfterViewInit, OnInit {
      constructor(private location: Location, private router: Router) { 
          setInterval(() =&gt; {
              this.now = new Date();
          }, 1000);
      }
  ...
      onKeyDown(event: KeyboardEvent): void {
          event.preventDefault();
          if (event.which === this.KEY_ESCAPE){
              if (this.router.url !== '/'){
                  this.location.back();
              }
          }
      }
  </code></pre>
  
  <p>App.component.spec.ts:</p>
  
  <pre><code>import { TestBed, async } from '@angular/core/testing';
  import { AppComponent } from './app.component';
  import { HeaderComponent } from './header/header.component';
  import { RouterOutlet } from '@angular/router';
  
  describe('AppComponent', () =&gt; {
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          HeaderComponent,
          RouterOutlet
        ],
      }).compileComponents();
    }));
  
    it('should create the app', () =&gt; {
      const fixture = TestBed.createComponent(AppComponent);
      const app = fixture.debugElement.componentInstance;
      expect(app).toBeTruthy();
    });
  </code></pre>
  
  <pre><code>AppComponent should create the app
  Error: StaticInjectorError(DynamicTestModule)[HeaderComponent -&gt; Location]: 
    StaticInjectorError(Platform: core)[HeaderComponent -&gt; Location]: 
      NullInjectorError: No provider for Location!
  </code></pre>