Why is the code coverage missing for lines where inject() is used in functional ngrx effect?

Discussão em 'Angular' iniciado por kristof, Novembro 13, 2024.

  1. kristof

    kristof Guest

    I am working on an Angular project and using karma.js with jasmine to write and execute unit tests.

    My problem is that if I want to unit test the functional ngrx effect without the TestBed, the coverage report reports the lines as uncovered where I inject the dependencies to my effect function.

    I have created a dummy effect to demonstrate the issue.

    Actions:

    export const MyActions = createActionGroup({
    source: 'Dummy',
    events: {
    'Some action': emptyProps(),
    },
    });


    Effect:

    export const dummy = createEffect(
    (actions$ = inject(Actions)) =>
    actions$.pipe(
    ofType(MyActions.someAction),
    tap(() => console.log('inject line is not covered :(')),
    ),
    { functional: true, dispatch: false },
    );


    Unit test:

    it('should run as an example', (done: DoneFn) => {
    const actionsMock$ = of(MyActions.someAction());

    dummy(actionsMock$).subscribe(() => {
    expect(true).toBe(true);
    done();
    });
    });


    Coverage report as an image (shows in red that the line is not covered):

    [​IMG]

    I have found that using the TestBed the mentioned issue can be avoided as stated in this github issue: https://github.com/ngrx/platform/issues/3668#issuecomment-2093221356, but I would like to keep TestBed out of unit testing the ngrx effects for simplicity.

    Continue reading...

Compartilhe esta Página