<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vince</title>
    <description>The latest articles on DEV Community by Vince (@aturingmachine).</description>
    <link>https://dev.to/aturingmachine</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F97495%2Fcafac27b-3da7-4fbb-92bd-281967b7ed9b.gif</url>
      <title>DEV Community: Vince</title>
      <link>https://dev.to/aturingmachine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aturingmachine"/>
    <language>en</language>
    <item>
      <title>Testing RxJS with Marbles</title>
      <dc:creator>Vince</dc:creator>
      <pubDate>Mon, 13 Apr 2020 18:00:46 +0000</pubDate>
      <link>https://dev.to/aturingmachine/testing-rxjs-with-marbles-3112</link>
      <guid>https://dev.to/aturingmachine/testing-rxjs-with-marbles-3112</guid>
      <description>&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;As we learned in our first installment of the Learning RxJS series, RxJS is a reactive programming library. RxJS makes use of Observables, defined in the RxJS documentation as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A representation of any set of values over any amount of time. This is the most basic building block of RxJS. &lt;br&gt;
  &lt;a href="https://rxjs-dev.firebaseapp.com/api/index/class/Observable"&gt;-source&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So Observables are asynchronous, and represent a stream of values that are the result of an async operation. Anyone who has wrapped implementation code in an Angular project with a &lt;code&gt;setTimeout()&lt;/code&gt; knows that testing that code in a &lt;code&gt;fakeAsync()&lt;/code&gt; can cause some headaches, so you may be timid on putting the time into learning RxJS knowing you are adding more complex async code you'll have to test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Marbles?
&lt;/h2&gt;

&lt;p&gt;Marbles testing is the idea of breaking our Observables down into easy to read diagrams that show the passage of time for a specific Observable. They allow us to create rather easy to debug tests for complex, async, Observable based code. Lets look at the problem we are trying to solve. &lt;/p&gt;

&lt;p&gt;Assume we have a simple piece of implementation code, a component that consumes some service that will be making an async call. Using the default &lt;code&gt;HttpClient&lt;/code&gt; for Angular that call will return an Observable that we will need to consume in a component. That would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;someUrl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MyService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this simple example our &lt;code&gt;MyComponent&lt;/code&gt; is making a call into &lt;code&gt;MyService&lt;/code&gt;, who makes an HTTP request. However that service returns the Observable of that call, so our component subscribes and stores that value off. Testing this extremely simple service code would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyService&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return a get request to someUrl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;httpSpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that we are subscribing to the Observable returned by the service and storing that in a test scoped variable in order to test that value. We are loosely asserting that the value we push into the &lt;code&gt;httpSpy&lt;/code&gt; is returned as an Observable from the service, and setting ourselves up for failure if this code were to grow more complex. We would need to do more and more work within the spec managing a &lt;code&gt;fakeAsync&lt;/code&gt; timeline. Not to mention adding some common piped values to the &lt;code&gt;HttpClient&lt;/code&gt; call such as a &lt;code&gt;retry()&lt;/code&gt; or &lt;code&gt;timeout()&lt;/code&gt; can easily make this code a pain to test and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Marbles
&lt;/h2&gt;

&lt;p&gt;A Marble Diagram is a simple string based diagram for representing the state of an Observable over time, they look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-a--b-(c|)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bumblebee&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tiger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry too much about the symbols used or what &lt;code&gt;cold&lt;/code&gt; means, we will take a look at those in a minute.&lt;/p&gt;

&lt;p&gt;Marbles essentially allows us to write the future of an Observable, which we can then return from a spy to be consumed by our implementation code. This is extremely powerful, especially so, when our implementation is going to be modifying/&lt;code&gt;pipe()&lt;/code&gt;-ing that Observable and operating on it in some way; more on that in a minute. Lets take a look at how we construct a marble diagram.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: When referring to marbles diagrams this artice is directly referring to the &lt;code&gt;jasmine-marbles&lt;/code&gt; library recommended by the RxJS team. The official documentation can be found &lt;a href="https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Hot and Cold
&lt;/h3&gt;

&lt;p&gt;There are two types of marbles that we can create, &lt;code&gt;hot()&lt;/code&gt; and &lt;code&gt;cold()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;hot()&lt;/code&gt; marbles create a hot observable that immediately begins emitting values upon creation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cold()&lt;/code&gt; marbles create a cold observable that only start emitting once they are consumed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the time you will be creating &lt;code&gt;cold()&lt;/code&gt; Observables within your tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Marbles Dictionary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt; - The dash is used to represent &lt;strong&gt;one "frame" of time, generally 10ms passing.&lt;/strong&gt; &lt;em&gt;(this value may be different depending on the library being used and whether or not the marble is run within the &lt;code&gt;testScheduler.run()&lt;/code&gt; callback)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt; - The hash is used to represent &lt;strong&gt;an error being thrown by the Observable.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|&lt;/code&gt; - The pipe is used to represent &lt;strong&gt;the Observable completing.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;()&lt;/code&gt; - The parentheses are used to represent &lt;strong&gt;events occuring on the same frame.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt; - Any alphabetical letter represents &lt;strong&gt;an emitted value.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;100ms&lt;/code&gt; - A number followed by &lt;code&gt;ms&lt;/code&gt; represents &lt;strong&gt;a passage of time.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;whitespace&lt;/code&gt; - Any and all whitespace is &lt;strong&gt;ignored in a marble diagram, and can be used to help visually align multiple diagrams.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also some subcription specific characters we can make use of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; - The caret represents a &lt;strong&gt;subscription start point.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!&lt;/code&gt; - The bang represents a &lt;strong&gt;subscription end point.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Emitting Values
&lt;/h4&gt;

&lt;p&gt;Now that we know how to create a marble, lets look at how we emit values in a marble. Assume we need to emit a value of &lt;code&gt;'catseye'&lt;/code&gt; and then emit a specific error of the string &lt;code&gt;'Oops!'&lt;/code&gt; in order to test some logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-a-#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Oops!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first parameter is our diagram, here saying that after one frame of radio silence we emit some value &lt;code&gt;a&lt;/code&gt;, then go quiet for another frame, finally on our fourth frame we throw an error.&lt;/p&gt;

&lt;p&gt;The second parameter is an object containing our emitted values where the object's key is the character we used in the diagram, in our case &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The third parameter is the value of the error, which we decided in our test case needed to be the string &lt;code&gt;'Oops!'&lt;/code&gt;. Lets look at another, more complex diagram example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-a--b 100ms (c|)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are emitting value &lt;code&gt;a&lt;/code&gt; on frame 2, value &lt;code&gt;b&lt;/code&gt; on frame 5, then waiting 100ms. Then in a single frame our marble will emit value &lt;code&gt;c&lt;/code&gt; and complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Marbles Tests
&lt;/h2&gt;

&lt;p&gt;Lets look at the service example from above, with a slight modification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;someUrl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are making that same Get request as we were before, but we are telling the Observable to timeout if no result is recieved within 5 seconds, and retry that call twice, returning &lt;code&gt;undefined&lt;/code&gt; if we still fail after retrying. This is a pretty common pattern for HttpRequests that can fail silently in an Angular application, and not that fun to test using the traditional &lt;code&gt;subcribe()&lt;/code&gt; methodology shown above. Marbles are here to save the day!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;makeACall&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return the value from someUrl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;httpSpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expected$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toBeObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expected$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should retry twice on error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;httpSpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;returnValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
      &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
      &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expected$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;---e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;catseye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toBeObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expected$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should have a timeout of 5 seconds and return undefined on error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;httpSpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;and&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;- 5000ms&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expected$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;- 15000ms e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeACall&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toBeObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expected$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All we need to do to make sure the source and expected Observables are working on the same timeline, is to line up the diagrams in terms of frames and timed waits.&lt;/p&gt;

&lt;h4&gt;
  
  
  A Note on Developer Experience
&lt;/h4&gt;

&lt;p&gt;As we can see in the above examples we are creating an easily recreatable testing pattern. In order to understand the case all we need to do is look at the string pattern within the "source" returned by the &lt;code&gt;httpSpy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Marbles has allowed us to test more complex logic using the same pattern in all of our tests. Establishing patterns in your tests allows other developers to more easily write tests for new implementation code (and help you when you come back to that service you wrote 6 months ago).&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Marbles testing gives us a rich shared language for testing Observables and creating easy to extend testing patterns. We can also test more complex RxJS code without getting lost in the weeds of how to test it. Overall we are able to write better tests that are easier to understand, improving the Developer Experience and allowing us to move faster without sacrificing code quality.&lt;/p&gt;

&lt;p&gt;If you have any questions about using marbles in real practice, marbles in general, or the wider world of RxJS drop them in the comments below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing"&gt;The official RxJS docs on marbles testing&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;These docs refer to using the &lt;code&gt;testScheduler.run()&lt;/code&gt; callback, so the examples may look a bit different but are equally valid.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rxjs</category>
      <category>javascript</category>
      <category>angular</category>
      <category>testing</category>
    </item>
    <item>
      <title>An Introduction to RxJs Observables</title>
      <dc:creator>Vince</dc:creator>
      <pubDate>Wed, 12 Jun 2019 11:39:52 +0000</pubDate>
      <link>https://dev.to/aturingmachine/an-introduction-to-rxjs-observables-52ln</link>
      <guid>https://dev.to/aturingmachine/an-introduction-to-rxjs-observables-52ln</guid>
      <description>&lt;h1&gt;
  
  
  Understanding RxJs
&lt;/h1&gt;

&lt;p&gt;Reactive Programming can be extremely difficult to understand. Here is a quick introduction to RxJs to hopefully get you started using reactive programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is An Observable?
&lt;/h2&gt;

&lt;p&gt;An Observable is, in the simplest form, the result of an asynchronous operation. We can use them in place of Promises for a lot of asynchronous tasks. However an Observable allows for us to do complex logic on an async data stream with only a few lines of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Basics
&lt;/h2&gt;

&lt;p&gt;Before we get to the complex work we can do with Observables we should first understand the basics. There are a lot of helper methods to create Observables, for this example we will make use of &lt;code&gt;interval(period: number)&lt;/code&gt;. It creates an Observable that returns an incremented number every &lt;code&gt;period&lt;/code&gt; milliseconds. Creating this observable is as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Observable will "emit", the term used for when an Observable produces a new value, the following &lt;code&gt;1 (one second) 2 (one second) 3...&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subscriptions
&lt;/h3&gt;

&lt;p&gt;In order to get the emitted values from the above Observable we will need to "subscribe" to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will print out the emitted values as they are emitted from the Observable.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Idea of the River
&lt;/h3&gt;

&lt;p&gt;I find that when working with Observables it often helps to think of the "source" Observable as a river, with each emission being a boat that is floating down the river. By Subscribing to an Observable we are being given access to see the boats that are on the river. Next we will learn how to manipulate how and when those boats are perceived by someone watching the river.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Pipe Operator
&lt;/h2&gt;

&lt;p&gt;Here we are going to get into the more complex things we can do with Observables. We can achieve this using the &lt;code&gt;pipe()&lt;/code&gt; function that exists on an Observable. Taking the &lt;code&gt;source&lt;/code&gt; from above we can create a piped observable that only passes along even numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print out &lt;code&gt;2 ... 4 ... 6 ... etc&lt;/code&gt;. We can see that the Observable has operators that can act upon the emitted values. Here we use &lt;code&gt;filter(select: Function)&lt;/code&gt; to only accept values that are even. This is similar to the &lt;code&gt;filter()&lt;/code&gt; function on arrays in JavaScript.&lt;/p&gt;

&lt;p&gt;There are a load of pipeable operators that we can make use of.&lt;br&gt;
Assume we have an Observable who's source is click events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an Observable who will emit every time there is a &lt;code&gt;click&lt;/code&gt; event on the page.&lt;/p&gt;

&lt;p&gt;Now say we need collect these click events into batches of 5 and then send them off to an arbitrary API for processing, which will then return a value from that processing which we need to print out, we will assume we have a service written that is ready to make the API call since that is out of the scope of this article. Here we can make use of the &lt;code&gt;bufferCount(bufferSize: number, startBufferEvery: number = null)&lt;/code&gt; to do this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bufferCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;bufferCount(5)&lt;/code&gt; will collect 5 emissions from &lt;code&gt;source&lt;/code&gt; and then emit them as an array. Now that we have our events batched we need to send them off to the API. Our service will return an Observable from its service call, so we need to take the value from our source and pass it to a function that makes a new Observable, and then return the new Observable. We can make use of the &lt;code&gt;mergeMap()&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;bufferCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myAPIService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;processedEvents&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;processedEvents&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a few lines of RxJs operators and functions we have created some, albeit odd, logic that could take many more lines to complete with Promises.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;RxJs is an extremely powerful tool that can be extremely difficult to grasp, especially when working with large applications that retrieve data from multiple locations. I hope this article has helped shed some light on how Observables work. Feel free to drop any comments, questions, or concerns in the comments below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Resources
&lt;/h3&gt;

&lt;p&gt;When I am working with Observables I often check &lt;a href="//www.learnrxjs.io"&gt;learnrxjs.io&lt;/a&gt;. They have a list of Operators with examples and explanations of the operator. There is also a list of recipes showing the operators in action.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://rxjs.dev/api"&gt;official docs&lt;/a&gt; also contain useful information including marbles diagrams, which we will cover in the next article, and examples.&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>javascript</category>
      <category>angular</category>
      <category>reactivex</category>
    </item>
    <item>
      <title>How I Vim</title>
      <dc:creator>Vince</dc:creator>
      <pubDate>Fri, 24 May 2019 21:36:36 +0000</pubDate>
      <link>https://dev.to/aturingmachine/how-i-vim-4ha6</link>
      <guid>https://dev.to/aturingmachine/how-i-vim-4ha6</guid>
      <description>&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--VLm2P1sn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1071055431215276033/U9-RIlDs_normal.jpg" alt="I Am Devloper profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        I Am Devloper
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @iamdevloper
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O8zQMJiw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-b32530dfdcd41c7dfe6db7c499483db80a5cd1698cb25021231cd7da7620824b.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      23:26 PM - 17 Feb 2014
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=435555976687923200" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=435555976687923200" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      13829
      &lt;a href="https://twitter.com/intent/like?tweet_id=435555976687923200" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      8673
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Vim jokes are as old as the editor itself. However I have been using Vim daily for years now, mostly for quick edits and writing commit messages. I figured I would share my setup and maybe someone else could find it helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ease of Use
&lt;/h2&gt;

&lt;p&gt;The hardest thing for people to grasp when they first use Vim is the command system. This system can be extremely powerful, but for most use cases it just confuses new users on how to save the buffer they just wrote. Our first goal will be to abstract some of the more common commands to a hotkey combination.&lt;/p&gt;

&lt;p&gt;First we need to create a &lt;code&gt;.vimrc&lt;/code&gt; in our home directory &lt;code&gt;touch .vimrc&lt;/code&gt;, since you may not be a vim user yet make the following changes with whatever editor you prefer.&lt;/p&gt;

&lt;p&gt;We are going to be setting a "leader" which will be the start of all our hotkeys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; mapleader &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"`"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I prefer to use the&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;`&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 key since it is a bit out of the way and I am less likely to accidentally hit it, however you can set it to whatever you want.&lt;/p&gt;

&lt;p&gt;Now we are going to create a command to write the current buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;noremap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;noremap&lt;/code&gt; is a mapping command that is not recursive, meaning we will not be chaining our new commands together. This can get a bit unwieldy for beginners and make it hard to debug. If you are comfortable with vim then feel free to use the recursive method &lt;code&gt;map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we tell vim that we are mapping something we use the &lt;code&gt;&amp;lt;leader&amp;gt;&lt;/code&gt; tag to use that variable followed by &lt;code&gt;w&lt;/code&gt;. So the keystrokes&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;` w&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 will write the current buffer via the command &lt;code&gt;:w&lt;/code&gt;, executed by the &lt;code&gt;&amp;lt;cr&amp;gt;&lt;/code&gt; carriage return. This one line is the equivalent of pressing &lt;code&gt;esc&lt;/code&gt;, typing &lt;code&gt;:w&lt;/code&gt;, and then hitting &lt;code&gt;enter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we can write some more common commands via &lt;code&gt;noremap&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;nnoremap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;a&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;wqall&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
nnoremap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;q&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;qall&lt;/span&gt;&lt;span class="p"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;cr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These two mappings will add&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;` a&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 to write quit all buffers that are open, and&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;` q&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 to quit all open buffers.&lt;/p&gt;

&lt;p&gt;Now we can easily get out of vim!&lt;/p&gt;
&lt;h2&gt;
  
  
  Going Further
&lt;/h2&gt;

&lt;p&gt;Now that we have some commands mapped out to easily write and quit vim we can start to make it feel more like home. Here is an excerpt from my own &lt;code&gt;.vimrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="c"&gt;" Turn on syntax highlightin&lt;/span&gt;
syntax &lt;span class="k"&gt;on&lt;/span&gt;
&lt;span class="c"&gt;" Turn on line numbers&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="k"&gt;number&lt;/span&gt;
&lt;span class="c"&gt;" Set the colorscheme to elflord&lt;/span&gt;
&lt;span class="c"&gt;" See more colorschemes by entering command mode via esc then typing&lt;/span&gt;
&lt;span class="c"&gt;" :colorscheme then pressing space and changing it via tab&lt;/span&gt;
&lt;span class="k"&gt;colorscheme&lt;/span&gt; elflord

&lt;span class="c"&gt;" Turn tabs into spaces and set them to be two spaces long&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; expandtab
&lt;span class="k"&gt;set&lt;/span&gt; tabstop&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; softtabstop&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; shiftwidth&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; splitright

&lt;span class="c"&gt;" highlight the current line of the buffer&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; cursorline
&lt;span class="c"&gt;" allow the mouse to be used to change cursor position&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; mouse&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;a&lt;/span&gt;

&lt;span class="c"&gt;" This block here executes the Vim Explorer on startup, opening the&lt;/span&gt;
&lt;span class="c"&gt;" current directory tree in a small pane to the left&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:netrw_banner&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:netrw_liststyle&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:netrw_browse_split&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:netrw_altv&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:netrw_winsize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
augroup ProjectDrawer
  autocmd&lt;span class="p"&gt;!&lt;/span&gt;
  autocmd &lt;span class="nb"&gt;VimEnter&lt;/span&gt; * &lt;span class="p"&gt;:&lt;/span&gt;Vexplore
augroup END

&lt;span class="c"&gt;" `` should rotate split focus&lt;/span&gt;
noremap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;W&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this has possibly helped you better work around vim, or piqued your interest to start customizing your own vim to your liking!&lt;/p&gt;

&lt;p&gt;If you have any cool vim tips feel free to drop them in the comments, I'd love to up my vim game!&lt;/p&gt;

</description>
      <category>vim</category>
      <category>productivity</category>
      <category>bash</category>
      <category>texteditor</category>
    </item>
    <item>
      <title>My First Open Source Project</title>
      <dc:creator>Vince</dc:creator>
      <pubDate>Mon, 19 Nov 2018 13:28:11 +0000</pubDate>
      <link>https://dev.to/aturingmachine/my-first-open-source-project-n5n</link>
      <guid>https://dev.to/aturingmachine/my-first-open-source-project-n5n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Open Source can be daunting for new developers to get in to. Here are some tips that I have learned along the way while managing an, albeit small, active project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Background
&lt;/h1&gt;

&lt;p&gt;This project started around the time I was getting ready to graduate college, like most good projects it started because I was lazy. I had grown tired of constantly boiler-plating projects, so I made the &lt;a href="https://github.com/aturingmachine/mevn-stack"&gt;MEVN Stack&lt;/a&gt;. A vanilla quick start template of my preferred technologies with some example code in there to help get others (and myself) going on a new full stack project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Beast of Open Source
&lt;/h2&gt;

&lt;p&gt;Originally the project was made for myself and a few colleagues, however it started to get some traction from other developers on GitHub. Before I knew it the project was averaging 400-700 views every 2 weeks and had over 50 stars. With these new eyes on the project came contributions, and for me one of the scariest things. &lt;/p&gt;

&lt;p&gt;People looking at my code.&lt;/p&gt;

&lt;p&gt;When the project's &lt;a href="https://github.com/aturingmachine/mevn-stack/issues/1"&gt;first issue&lt;/a&gt; was opened my heart sank, I expected a programmer much better than myself to be tearing apart every aspect of my project. Turns out it was just a friend of mine calling out some redundant code that snuck in, a completely humbling experience. Since then the project has gained 2 more contributors, merged 2 pull requests, and closed 2 more issues. These are some things I learned along the way:&lt;/p&gt;

&lt;h3&gt;
  
  
  Mindset
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;It is easy to get a solid case of imposter syndrome when putting your work out for the world to see. Here are some tips for how to think when approaching Open Source:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Be humble.&lt;/strong&gt;&lt;br&gt;
When my first issue was calling out a simple redundant line of code I realized that 99% of the time if someone is opening an issue, they are commenting on your work in good faith.  Issues are not where someone is going to call you out and tell you that you are not a real developer. The issue is more often than not a chance to improve the project, and yourself as a developer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep an open mind.&lt;/strong&gt; &lt;br&gt;
If someone is &lt;a href="https://github.com/aturingmachine/mevn-stack/issues/4"&gt;reporting an issue that you cannot reproduce&lt;/a&gt;, attempt to gather more information. If they propose a code change, ask them to open a Pull Request. Review the changes they made, perhaps it does not affect the project on your environment, but will improve it on theirs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Product
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;When most user's click on your project the only up front information they have is the name, and a README (hopefully). These tips pertain to the actual product, your code:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write readable code.&lt;/strong&gt; &lt;br&gt;
When someone pops open the source code of your project they should be able to read and understand what it is doing. Not only do I mean that your code should be formatted properly, it should also be understood. Give variables and functions meaningful names, follow (or at least loosely follow) standards that people are expecting to see. Do your best to remove the mystery around your source code, this will make it easier for others to use and help them over the hurdle of starting to contribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep it simple.&lt;/strong&gt;&lt;br&gt;
This is applicable in most cases. If there is no benefit to using the super convoluted function you just thought up, then don't. Every point in your code that makes it harder for another dev to "compile" in their head, is another point you could lose a potential contributor. In cases where simplicity is unattainable, leave a comment in your code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Self-Care
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;When people are using your code you may feel obligated to carve time out of your day to fix problems they are having, or improving their experience. You should do these things, but not at the expense of your own health. These are some things I learned about putting yourself first:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't overcommit.&lt;/strong&gt;&lt;br&gt;
When working in Open Source, people will ask for improvement, help, and bug fixes. It is easy to get in the mindset of "Someone asked for X, I have to drop everything and do it". However &lt;em&gt;you&lt;/em&gt; come first, if you do not have the time due to other commitments, or are starting to burn out a bit; ask the reporter if they would like to submit the fix. This helps you keep a balance, helps the project remain maintained, and helps someone else contribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Find a balance.&lt;/strong&gt;&lt;br&gt;
I work 40+ hours a week along with a long commute and travel. Finding time to work on projects can be a bit difficult. The best I have found is taking a chunk of time during a vacation or long weekend to get some tasks done. This works well &lt;em&gt;for me&lt;/em&gt;, I get to hit a groove and check off a bunch of work without it encroaching too much into the rest of my time off. Find what works best for you and stick to it, if someone is asking you to do some work and not respecting your timetables then they are not coming to you in good faith.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;I hope these tips I picked up in the past year of working in Open Source is helpful to developers looking to get their foot in the door and seasoned contributors alike. If you have any more helpful hints to contributing feel free to drop them in the comments below!&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>git</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
