spyOn isDevMode() in Angular 6+ Unit Tests?

Discussão em 'Angular' iniciado por Jeremy Moritz, Setembro 11, 2024.

  1. 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?

    Continue reading...

Compartilhe esta Página