spyOn isDevMode() in Angular 6+ Unit Tests?

My code includes an if block like this

Service:

import { isDevMode } from '@angular/core';

export class MyService {
  constructor() {}

  methodToTest(): {
    if (!isDevMode()) {
      doSomething();
    } else {
      doSomethingDifferentInDevMode();
    }
  }
}

my-service.spec.ts (the relevant lines only):

it('should run doSomething() when isDevMode() returns false', () => {
  // what should i do here to mock a false value return for isDevMode() ?
  expect(doSomething).toHaveBeenCalled();
});

How do i spyOn the isDevMode() to make it return false for this single Angular unit test so that i may test that doSomething() is called?

2 answers

  • answered 2018-07-18 01:54 robrich

    Looks like the answer is in the comments. Thanks @JBNizet.

    // dev-mode-service.ts
    
    import { isDevMode } from '@angular/core';
    
    export class DevModeService {
      constructor() {}
    
      isDevMode(): {
        return isDevMode();
      }
    }
    
    
    // MyService.ts
    
    import { isDevMode } from 'dev-mode-service';
    
    export class MyService {
      constructor() {}
    
      methodToTest(): {
        if (!isDevMode()) {
          doSomething();
        } else {
          doSomethingDifferentInDevMode();
        }
      }
    }
    

  • answered 2018-07-18 06:04 JB Nizet

    Start by wrapping this function into a service:

    import { isDevMode, Injectable } from '@angular/core';
    
    @Injectable({ providedIn: 'root' })
    export class DevModeService {
      constructor() {}
    
      isDevMode() {
        return isDevMode();
      }
    }
    

    Then you can inject this service wherever you want, as you would do with any other service:

    import { DevModeService } from 'dev-mode.service';
    
    export class MyService {
      constructor(private devModeService: DevModeService) {}
    
      methodToTest(): {
        if (!this.devModeService.isDevMode()) {
          doSomething();
        } else {
          doSomethingElse();
        }
      }
    }
    

    And you can then spy on the DevModeService when testing MyService, like you would do with any other dependency:

    it('should do something when in dev mode') {
      const devModeService: DevModeService = TestBed.get(DevModeService);
      spyOn(devModeService, 'isDevMode').and.returnValue(true);
    
      const myService: MyService = TestBed.get(MyService);
      myService.methodToTest();
      // ...
    }
    
    it('should do something else when in prod mode') {
      const devModeService: DevModeService = TestBed.get(DevModeService);
      spyOn(devModeService, 'isDevMode').and.returnValue(false);
    
      const myService: MyService = TestBed.get(MyService);
      myService.methodToTest();
      // ...
    }