question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve Observables Testing documentation

See original GitHub issue

πŸ“š Docs or angular.io bug report

Description of the problem

Observables are a huge part of Angular.

Currently, the testing docs have examples on how to test Observable related code scattered across docs and we don’t have a dedicated place to learn about Observable testing.

A lot of examples are scattered around the docs and suggest either testing observables by subscribing (without unsubscribing BTW which is another issue), or showing how to test using marble tests, (and doing it only in the β€œcomponents section” although it’s not limited only to components). And by that it seems like we’re jumping from the β€œeasiest thing possible” straight to β€œhaving to know hot, cold, TestScheduler and look at all these signs!” type of tests (marble testing).

When there is a much simpler way to test Observables. It pretty new but very effective (according to people who used it in real life) and it’s called:

observer-spy

I created it a few months ago with the aim of reducing complexity when it comes to testing observables.

So although I’m biased, I waited a few months to see whether this open source library actually provides value to devs other than my clients before I thought about suggesting to add it to the docs.

So far it seems that it makes people lives’ easier.

For example, here are some of the responses I got since it got published -


image


image


image


Observer spies are much less complicated than marble testing and don’t require testing implementation details like knowing exactly how many frames passed between values (which normally you only care about the values and their order, rather than the time between them).

I believe marble testing should be the last topic we cover on Observable Testing to reduce the friction newcomers have when trying to learn Angular + testing, and to supply them with the simplest tools first before jumping into the more complicated ones πŸ˜…

Proposed Solution - a Dedicated β€œObservables Testing” Document

Because Observables are a cross cutting topic which can be found in all Angular types (services, components, etc), I believe it should have it’s own document called β€œObservables Testing”.

In there we should start by showing the simplest way (subscribing to get a value), then showing how to use observer-spies and with subscribeSpyTo and autoUnsubscribe (not to leave any leaks), the move to show how to auto-spy on dependencies which return observables and lastly - show how to test using marble tests.

The proposed table of contents:

  1. An Observable Under Test - the easiest way
  2. Testing multiple values - using observer spies
  3. Faking dependencies with Observable returning methods
  4. Marble Tests

I believe this would make the whole notion of Observable testing much easier to digest and get easy wins in both Angular and also NgRx code (like testing effects) and will help lower the entry barrier for testing Observables.

Let me know what you think. If we move forward, I could create a draft of the content and work with your documentation team to include it on Angular.io.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:32
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jessicajaniukcommented, Mar 15, 2022

@shairez Yes, as we shouldn’t be documenting it. It’ll get out of date as it’s not our library and is just a dependency. We’d also welcome a community PR to remove this documentation.

0reactions
angular-automatic-lock-bot[bot]commented, Apr 15, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJS Testing β€” Write Unit Tests for Observables
RxJS comes with a handy toolset to test observables using marble diagrams as ... You can find the documentation about them here: ......
Read more >
A quick tip on testing Observables πŸ‘€ | by Angel Nikolov
Test asynchronous Observables as if they were synchronous! ... Improve your Angular app performance by using this simple Observable cacheΒ ...
Read more >
Writing Tests
Learning to use and write marble tests can be tough. While learning, keep in mind ... Many testing frameworks provide better mocking facilities...
Read more >
Mocking Observables in JavaScript Tests - TestProject
We present three different ways of testing Observables in JavaScript. ... is a better way to test how many times something was executed)....
Read more >
RxJs testing patterns - Technical Blog at ...
You can specify exactly which behavior is expected from the tested observable. With the use of mock functions, you can verify that observable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found