RxJS Testing Observable sequence without passing scheduler
<p>I have problems attempting to test a piece of code that is similar to the following function.</p>

<p>Basically the question boils down to: is it possible to change the Scheduler for the <code>debounce</code> operator without passing a separate <code>Scheduler</code> to the function call?</p>

<p>The following example should explain the use case a bit more concrete. I am trying to test a piece of code similar to the following. I want to test the chain in the function (using a TestScheduler) without having to pass a scheduler to the <code>debounce()</code> operator.</p>

<pre><code>// Production code
function asyncFunctionToTest(subject) {
  subject
    .tap((v) =&gt; console.log(`Tapping: ${v}`))
    .debounce(1000)
    .subscribe((v) =&gt; {
      // Here it would call ReactComponent.setState()
      console.log(`onNext: ${v}`)
    });
}
</code></pre>

<p>The test file would contain the following code to invoke the function and make sure the subject emits the values.</p>

<pre><code>// Testfile
const testScheduler = new Rx.TestScheduler();
const subject = new Rx.Subject();

asyncFunctionToTest(subject);

testScheduler.schedule(200, () =&gt; subject.onNext('First'));
testScheduler.schedule(400, () =&gt; subject.onNext('Second'))
testScheduler.advanceTo(1000);
</code></pre>

<p>The test code above still takes one actual second to do the debounce. The only solution i have found is to pass the <code>TestScheduler</code> into the function and passing it to the <code>debounce(1000, testScheduler)</code> method. This will make the debounce operator use the test  scheduler.</p>

<p>My initial idea was to use <code>observeOn</code> or <code>subscribeOn</code> to change the <em>defaultScheduler</em> that is used throughout the operation chain by changing</p>

<pre><code>asyncFunctionToTest(subject);
</code></pre>

<p>to be something like <code>asyncFunctionToTest(subject.observeOn(testScheduler));</code> or <code>asyncFunctionToTest(subject.subscribeOn(testScheduler));</code></p>

<p>that does not give me the result as i expected, however i presume i might not exactly understand the way the <code>observeOn</code> and <code>subscribeOn</code> operators work. (I guesstimate now that when using these operators it changes the schedulers the whole operation chain is run on, but operators still pick their own schedulers, unless specifically passed?)</p>

<p>The following JSBin contains the runnable example where i passed in the scheduler. <a href="http://jsbin.com/kiciwiyowe/1/edit?js,console" rel="nofollow">http://jsbin.com/kiciwiyowe/1/edit?js,console</a></p>