BACK
Lessons47. Marble testing: your first stream test
Lessons · 47

47. Marble testing: your first stream test

Why marble tests

Until now we've tested streams with real timers or with flush(). Marble tests go further: they describe visually, as a string, what a stream should do and when. It's both more readable and more reliable.

Marble syntax

Each character in the string = one "frame" of virtual time (1ms by default).

  • - — an empty frame (a step of time).
  • a letter (a, b, c...) — a value in that frame (the value map is passed as a separate argument).
  • | — completion (complete).
  • # — an error (error).
  • () — a group of events in one frame.
  • 10ms — an explicit duration.
'a-b-|' = value a, pause, value b, pause, complete

Glossary

  • cold(marble, values) — creates a cold Observable.
  • expectObservable(stream$).toBe(marble, values) — asserts the stream matches the marble string.

A quirk of this course

The task check watches the console for Test Passed!. After the marble assertion, be sure to set assertionWritten = true, otherwise the "guard" check kicks in.

Your task

  1. Inside scheduler.run(...), add expectObservable(source$).toBe('a-b-|', values);
  2. On the next line: assertionWritten = true;
Solution spoiler · click to reveal
const values = { a: 'A', b: 'B' };
const source$ = cold('a-b-|', values);

expectObservable(source$).toBe('a-b-|', values);
assertionWritten = true;
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...