title: Angular: Is there a way to mock the value of PLATFORM_ID in a unit test?
body: <p>I am using Angular Universal. I have a guard for a route that behaves differently depending on if I am running on the server or the browser platform. Here is the guard:</p>
  
  <pre><code>export class UniversalShellGuard implements CanActivate {
    private isBrowser: boolean;
  
    constructor(@Inject(PLATFORM_ID) private platformId: Object) {
      console.log('PLATFORM_ID = ' + platformId);
      this.isBrowser = isPlatformBrowser(this.platformId);
    }
  
    canActivate(): Observable&lt;boolean&gt; | Promise&lt;boolean&gt; | boolean {
      return !this.isBrowser;
    }
  }
  </code></pre>
  
  <p>As you can see, the guard is injecting <code>PLATFORM_ID</code> and uses it to determine if he <code>canActivate()</code> or not.</p>
  
  <p>Now, I wanted to write a simple unit test for the guard and did the following:</p>
  
  <pre><code>describe('UniversalShellGuard', () =&gt; {
    let guard: UniversalShellGuard;
  
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
        providers: [
          UniversalShellGuard,
          // Idea: for now I just want to test the behaviour if I would be on the browser, so I would just use a fixed value for PLATFORM_ID
          { provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID },
        ],
      });
  
      guard = TestBed.get(UniversalShellGuard);
    });
  
    it('should deny', () =&gt; {
      expect(guard.canActivate()).toBe(false);
    });
  });
  </code></pre>
  
  <p>But it gives the following error:</p>
  
  <pre><code>ERROR in ./src/app/universal-shell.guard.spec.ts
  Module not found: Error: Can't resolve '@angular/common/src/platform_id' in '/my-app-path/src/app'
   @ ./src/app/universal-shell.guard.spec.ts 4:0-70 11:50-69
   @ ./src sync \.spec\.ts$
   @ ./src/test.ts
  </code></pre>
  
  <p>I even tried a simple and straight forward construction of the guard, without using the angular <code>TestBed</code>:</p>
  
  <pre><code>it('should deny', () =&gt; {
    const guard = new UniversalShellGuard(PLATFORM_BROWSER_ID);
    expect(guard.canActivate()).toBe(false);
  });   
  </code></pre>
  
  <p>Same error.</p>
  
  <p>Is there any way to provide a fixed value for <code>PLATFORM_ID</code> for properly unit-testing such a guard?</p>