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-|= onlyxandy, 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
- In
pipe, addfilter(value => value % 2 === 0), thenmap(value => value * 10). - Add
expectObservable(result$).toBe('---x---y-|', expectedValues); - 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
CONSOLE · Console output
Hit Run to see the result...