Creating Observables (Exercise)

We can write some tests to wrap our head around how the two, most basic ways to create observables work. Let's start with two incredibly simple tests.

Things to look out for here:

import { from, of } from 'rxjs';

describe('Basic Observables', () => {
  describe(of, () => {
    it('should create an observable from its arguments', () => {
      const example$ = of(1, 2, 3, 4);
      const result = [];

      example$.subscribe((value) => result.push(value));

      expect(result).toEqual([1, 2, 3, 4]);
    });
  });

  describe(from, () => {
    it('should create an observable', () => {
      const example$ = from([1, 2, 3, 4]);
      const result = [];

      example$.subscribe((value) => result.push(value));

      expect(result).toEqual([1, 2, 3, 4]);
    });
  });
});

Exercise

We have a set of tests in exercises/creating-observables.test.js. You can run just these tests using the the following.

npm test creating will scope Jest down to just the appropriate tests.

Your Mission: Un-skip each test and make sure they pass.

Some things that you will want to keep in mind: