Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive unit tests of observables #492

Open
TarasCZ opened this issue Oct 5, 2019 · 3 comments
Open

False-positive unit tests of observables #492

TarasCZ opened this issue Oct 5, 2019 · 3 comments

Comments

@TarasCZ
Copy link

TarasCZ commented Oct 5, 2019

Hello, I found that most of your unit tests that tests observable are false-positive, meaning that these tests will pass even when they are supposed to fail.

Minimal reproduction of the bug with instructions:

just change behavior of effect or put one false-positive except (except(false).toBeTruthy()) into the subscribe method and test will still pass

Expected behavior:

Test will fail when they are supposed to

Other information:

example:

   effect.login.subscribe(() => {
     expect(localStorageService.setItem).toHaveBeenCalledWith(AUTH_KEY, {
        isAuthenticated: false
      });
      expect(false).toBeTruthy(); // should obviously fail but it won't
      expect(router.navigate).toHaveBeenCalledWith(['']);
    });
  });

this test has its 'except' in subscribe method, which is asynchronous and will be run after the test will complete without error

I would be willing to submit a PR to fix this issue:

Yes, I would like to start working on PR if you agree.
I would also remove these cold and hot helpers because most of the time here we are testing one value emitted from the observable and there are not necessary.

@tomastrajan
Copy link
Owner

Hi @TarasCZ !

Thank you very much for finding this, it would be amazing to make this right! I would go with standard @rxjs/testing with TestScheduler which enables simple marble testing without real time!

Docs

Thanks mate!

@TarasCZ
Copy link
Author

TarasCZ commented Oct 7, 2019

Hey Tomas :)
I think your solution is bit of an overkill for these simple purposes, since we don't need to test observable behavior over time, also I think is confusing when there is such unnecessary test because ngrx is immutable and shouldn't depend on when the action is fired.
I think the KISS rule could be applied here, I propose use BehaviorSubject and done function like this:

unit-test

Let me know what do you think about this :)
Thanks, Jirka

@tomastrajan
Copy link
Owner

@TarasCZ hi Jirka!

Yeah testing of actions is maybe too much ... still having even simple test using TestScheduler can be used as a starting point on how to use it for more complex cases.

Timing of action DOES matter for more complex effects with delays, debounces, race and other complex orchestration which are part of real world applications :)

What do you say ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants