<?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: Noor Ul Haq</title>
    <description>The latest articles on DEV Community by Noor Ul Haq (@noor0).</description>
    <link>https://dev.to/noor0</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%2F111419%2Fecd090d6-8935-4a91-92ed-d011857fad75.jpeg</url>
      <title>DEV Community: Noor Ul Haq</title>
      <link>https://dev.to/noor0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/noor0"/>
    <language>en</language>
    <item>
      <title>Marble Testing With RxJS</title>
      <dc:creator>Noor Ul Haq</dc:creator>
      <pubDate>Wed, 15 Apr 2020 19:57:52 +0000</pubDate>
      <link>https://dev.to/noor0/marble-testing-with-rxjs-h06</link>
      <guid>https://dev.to/noor0/marble-testing-with-rxjs-h06</guid>
      <description>&lt;p&gt;I recently came across a nice way of testing observables called “marble testing”. It’s called marble testing because marble diagrams, the diagrams in the documentation of the library, are used to assert behavior and values, and create mock observables for the observable under test.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making a marble diagram in code
&lt;/h1&gt;

&lt;p&gt;Marble diagrams are usually pictures, but in our tests, a marble diagram is simply a string which looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const input$ = "--a----b--c|";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It represents events occurring over “virtual time”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;-&lt;/code&gt;: Represents a frame, and it is equal to 1ms of virtual time for our observable. It is possible to configure the amount of virtual time.&lt;br&gt;
&lt;code&gt;[a-z0-9]&lt;/code&gt;: Represents a value being emitted by the observable, and advances time by one frame.&lt;br&gt;
&lt;code&gt;(abc)&lt;/code&gt;: Groups multiple values that are expected to be emitted in a single frame. It also advances virtual time by number values emitted plus 2 for &lt;code&gt;()&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;[0-9](ms|s|m)&lt;/code&gt;: Represents amount of virtual time, you can use it as a replacement of -.&lt;br&gt;
&lt;code&gt;|&lt;/code&gt;: Represents a complete signal i.e., observable has completed and has nothing more to emit.&lt;br&gt;
&lt;code&gt;#&lt;/code&gt;: Represents an error being thrown from the observable.&lt;br&gt;
&lt;code&gt;^&lt;/code&gt;(only in ‘hot’ observables): Represents the point in time where a subscription is expected, and represent the 0 frame, so &lt;code&gt;--^--a--b--|&lt;/code&gt; shows that a subscription is expected at &lt;code&gt;^&lt;/code&gt;. The frames before &lt;code&gt;^&lt;/code&gt; are -ve, and the ones after it are +ve.&lt;br&gt;
&lt;code&gt;!&lt;/code&gt;: Represent unsubscription point.&lt;/p&gt;

&lt;p&gt;Note: Both &lt;code&gt;^&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt; can be used to assert when was an observable was subscribed and unsubscribed to, and also to guide when should the observable being tested be subscribed and unsubscribed to. I’ve added a few example that’ll make it more clear.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hot and Cold Observables
&lt;/h1&gt;

&lt;p&gt;Before we begin writing tests it is important that we understand the difference between hot and cold observables. There are a few ways to describe hot and cold observables, &lt;a href="https://stackoverflow.com/questions/2521277/what-are-the-hot-and-cold-observables" rel="noopener noreferrer"&gt;so I’d suggest reading up on it a bit here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The easiest explanation is that in a hot observable, the producer is not part of the observable and it emits values whether it has any subscribers or not, for example an observable over mouse move event.&lt;/p&gt;

&lt;p&gt;A cold observable emits values only when it is subscribed to; the producer is created when the observable is subscribed to, for example an &lt;code&gt;ajax&lt;/code&gt; request with ajax operator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@benlesh/hot-vs-cold-observables-f8094ed53339" rel="noopener noreferrer"&gt;Learn more about hot and cold observable&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Time to write some tests
&lt;/h1&gt;

&lt;p&gt;Let’s test an observable that emits two values with an interval of 10ms, increments them by 1 and then completes afterwards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2A2Qey1c9NK0XVMLMP6GMrcA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2A2Qey1c9NK0XVMLMP6GMrcA.png" alt="simple observable to test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Import &lt;code&gt;TestScheduler&lt;/code&gt; from &lt;code&gt;rxjs/testing&lt;/code&gt;, instantiate it with the function to perform the assertion. I’m using TestScheduler for simplicity but you can also use &lt;code&gt;rxjs-marbles&lt;/code&gt; or &lt;code&gt;jest-marbles&lt;/code&gt; for writing marble tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AsjRG4ACqE2vbuD2mE5doSg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AsjRG4ACqE2vbuD2mE5doSg.png" alt="import TestScheduler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s finally write our test. We can represent &lt;code&gt;input$&lt;/code&gt; behavior in marble diagram as &lt;code&gt;10ms a 9ms (b|)&lt;/code&gt;. Why is there a 9ms if the values are emitted after 10ms? because, just like &lt;code&gt;-&lt;/code&gt;, a symbol representing a value also advances the frame by &lt;code&gt;1ms&lt;/code&gt; of virtual time, so when &lt;code&gt;a&lt;/code&gt; will be emitted, 11ms of virtual time will have passed and because of that, second value &lt;code&gt;b&lt;/code&gt; will be emitted 9ms after &lt;code&gt;a&lt;/code&gt; and the observable will complete on that frame which is why the complete signal is grouped with &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AlpCR9IU3tKPVJuTUnmU6uA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AlpCR9IU3tKPVJuTUnmU6uA.png" alt="simple test for a simple observable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We passed a function to &lt;code&gt;scheduler.run()&lt;/code&gt; which will be called with some helpers to mock hot and cold observables for the observable under test, queue assertions etc. One these helpers is &lt;code&gt;expectObservable&lt;/code&gt;, we’ll use it to queue our assertion. Assertions are executed synchronously after our callback is executed. We can also run assertions while our callback is being executed by calling &lt;code&gt;helpers.flush()&lt;/code&gt; but &lt;code&gt;scheduler.run()&lt;/code&gt; does that for us anyway.&lt;/p&gt;

&lt;p&gt;Let’s write another test for an observable that subscribes over an observable of input events.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AKligOyPx24fBtwwxNOuOSg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AKligOyPx24fBtwwxNOuOSg.png" alt="another simple observable to test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;our test will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AEZguqnjn7cjvxnRy1CrdIw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AEZguqnjn7cjvxnRy1CrdIw.png" alt="another simple test for a simple observable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One more thing you can control, is when the TestScheduler subscribes to and unsubscribes from the observable under test. expectObservable helper takes a second string argument called “&lt;strong&gt;subscription marble&lt;/strong&gt;” that does that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2ASU6EjHs93xlNxRROQuvGRw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2ASU6EjHs93xlNxRROQuvGRw.png" alt="using subscription marble to subscribe and unsubscribe"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;subMarble&lt;/code&gt;, TestScheduler is being instructed to subscribe to &lt;code&gt;output$&lt;/code&gt; a frame before &lt;code&gt;input$&lt;/code&gt; emits any value and unsubscribe from &lt;code&gt;output$&lt;/code&gt; two frames after it emits its first value. Because of early unsubscription TestScheduler only receives one value i.e., &lt;code&gt;a&lt;/code&gt;, which is why we had to update &lt;code&gt;outputMarbles&lt;/code&gt; and values.&lt;/p&gt;

&lt;p&gt;Hopefully this post has given you enough understanding to start writing your tests and jump into &lt;a href="https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/testing/marble-testing.md" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
