Angular unit test of @ngrx/effects, actions not purged

Discussão em 'Angular' iniciado por Hilbert, Janeiro 10, 2025.

  1. Hilbert

    Hilbert Guest

    I defined a number of effects, whose first one has dispatch: false:

    readonly abort$ = createEffect(() => {
    return this.actions.pipe(
    ofType(myActions.abort),
    tap( (action) => {
    console.log('request abort for', action.reason.toString());
    ...
    }),
    catchError(MyEffects.catchServiceError)
    )
    }, { dispatch: false } );


    I defined unit tests like following:

    describe('MyBankEffects', () => {
    let effects: MyBankEffects;
    let action$: Observable<Action> = new Observable<Action>();
    beforeEach( () => {
    TestBed.configureTestingModule({
    imports: [
    EffectsModule.forFeature(MyBankEffects)
    ],
    providers: [
    provideMockActions( () => action$),
    ]
    });
    effects = TestBed.inject(MyBankEffects);
    });

    it('should navigate to abort', (done) => {
    action$ = of(myActions.abort({ reason: AbortReason.TIMEOUT }));
    effects.abort$.pipe(take(1)).subscribe({
    next: () => {
    expect(...);
    },
    error: e => done.fail(e),
    complete: () => {
    expect(...);
    done();
    }
    });
    });


    In the next executed test, whichever it is, the first effect is executed again (without any mocks defined in its specific test), before running the test itself. It looks like the action dispatched in the test above, remains "in queue", and is consumed by the following test execution.

    Anyone can explain what happens, and how can I purge the action at the end of the single test?

    Continue reading...

Compartilhe esta Página