BACK
Lessons48. Marble testing: testing map and filter
Lessons · 48

48. Marble testing: testing map and filter

A marble test checks behavior

A good marble test doesn't repeat the implementation. It states clearly: "here's the input, here's the expected output — the right logic must sit between them." If the logic changes, the test is still valid.

Reading the strings

  • Input -a-b-c-d-| = four values at equal intervals, then completion.
  • Output ---x---y-| = only x and y, at the same time positions as the source values that made it through the filter.
  • Position in the string matters: each character maps to the same position in virtual time.

Your task

  1. In pipe, add filter(value => value % 2 === 0), then map(value => value * 10).
  2. Add expectObservable(result$).toBe('---x---y-|', expectedValues);
  3. Set assertionWritten = true;
Solution spoiler · click to reveal
const { map, filter } = Rx;

const sourceValues = { a: 1, b: 2, c: 3, d: 4 };
const expectedValues = { x: 20, y: 40 };

const source$ = cold('-a-b-c-d-|', sourceValues);

const result$ = source$.pipe(
  filter(value => value % 2 === 0),
  map(value => value * 10)
);

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