1

Unit test is failing for ngrx effect.

Effect method code:

  addNewLedgerAccountRequest$ = createEffect(() =>
  this.actions$.pipe(
    ofType(AddNewLedgerAccountActions.AddNewLedgerAccountActionTypes.ADD_NEW_LEDGER_ACCOUNT_REQUEST),
    withLatestFrom(this.addNewLedgerAccountStore$.select(AddNewLedgerAccountSelectors.ledgerAccountFormSelector)),
    switchMap(([, addNewLedgerAccountForm]: [Action, LedgerAccountFormDef]) => {
      return this.addNewLedgerAccountService.addNewLedgerAccount$(addNewLedgerAccountForm).pipe(
        map((ledgerAccount: LedgerAccountDef) => new AddNewLedgerAccountActions.AddNewLedgerAccountSuccess({ ledgerAccount })),
        catchError(() => of(new AddNewLedgerAccountActions.AddNewLedgerAccountFailure()))
      );
    })
  )
);

Effect test method code:

describe(`AddNewLedgerRequest$`, () => {
    it(`should dispatch AddNewLedgerAccountRequest action if service succeeds`, () => {
      const action = new fromActions.AddNewLedgerAccountRequest();
      actions$ = hot('-a', { a: action });
      const outcome = new fromActions.AddNewLedgerAccountSuccess({ ledgerAccount: mockedLedgerAccount });
      const expected$ = cold('--b', { b: outcome });
      const result$ = cold( '-b', { b: mockedLedgerAccount } );
      addNewLedgerAccountSpy.mockReturnValue( result$ );
      expect( effects.addNewLedgerAccountRequest$ ).toBeObservable( expected$ );
      expect( addNewLedgerAccountSpy ).toHaveBeenCalledWith( mockedLedgerAccountForm );
    } );
  } );

Result:

        Expected: --b,
        Received: --?,

        Expected:
        [{"frame":20,"notification":{"kind":"N","value":{"payload":{"ledgerAccount":{"id":"1234","name":"TestLedger","number":"3344","type":"12","taxRate":"12","taxRatePercent":"12"}},"type":"[point - CONFIGURATION WIZARD] Add new ledger account - Success"},"hasValue":true}}]

        Received:
        [{"frame":20,"notification":{"kind":"N","value":{"payload":{"ledgerAccount":{"id":"1234","name":"TestLedger","number":"3344","type":"12","taxRate":"12","taxRatePercent":"12"}},"type":"[point - CONFIGURATION WIZARD] Add new ledger account - Success"}}}],

      86 |       const result$ = cold( '-b', { b: mockedLedgerAccount } );
      87 |       addNewLedgerAccountSpy.mockReturnValue( result$ );
    > 88 |       expect( effects.addNewLedgerAccountRequest$ ).toBeObservable( expected$ );
         |                                                     ^
      89 |       expect( addNewLedgerAccountSpy ).toHaveBeenCalledWith( mockedLedgerAccountForm );
      90 |     } );

The only difference is Expected has ,"hasValue":true and Received is missing it. Please let me know how to solve it. Thank you,

Cluadia Hedda
  • 121
  • 1
  • 7

1 Answers1

1

It might be due to the version of jasmine-marbles in pacakge.json

I was facing a similar issue. After spending too much time, I did not find any solution. Then I removed jasmine-marbles from Package.json.

And I reinstalled it by doing:

     npm install jasmine-marbles

After that these error did not appaear again.

I would suggest you to fry this.

H S W
  • 6,310
  • 4
  • 25
  • 36