tomwhite007 / withLastestFrom-mocking-issue

A basic scaffolded Angular app with NgRx to prove an issue with mocking RxJs withLatestFrom values inside a unit test

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

withLatestFrom Unit Test mocking issue

I created this project to demonstrate unexpected behaviour from the RxJs pipe operator, withLatestFrom, when its results are mocked within a test.

To prove this, I've created to NgRx effects that do the same job; one using withLatestFrom and one using switchMap (see file: dummy-state.effects.spec.ts). I have then created two identical unit tests that mock the method that returns the observable within them (see file: dummy-state.effects.ts).

Finally, to solve the problem, I've created my own operator withNextFrom which serves the same purpose for me as withLatestFrom, but doesn't need to be aware of the observable stream's history because it's only interested in the next value (or current one, in the case of Behaviour Subject).

History

I first noticed this when trying to create a demo using RxJs Marbles Testing library. I couldn't understand why my production effects could use Marbles and mocking inside each test, but my demo project could only be mocked on initialisation (rather than inside each test). After removing everything piece by piece, I found that the only difference in my Marbles test cases was that my demo NgRx effects were using withLatestFrom.

I found that withLatestFrom mocking on initialisation inside the beforeEach() works fine. But this is really considered bad practice because it separates the test case mocked values from the unit test

Getting started

This project was generated with Angular CLI version 8.3.18.

Use npm install to get dependencies.

Running unit tests

Run ng test to execute the unit tests via Karma.

About

A basic scaffolded Angular app with NgRx to prove an issue with mocking RxJs withLatestFrom values inside a unit test


Languages

Language:HTML 61.6%Language:TypeScript 33.8%Language:JavaScript 4.4%Language:CSS 0.2%