Rxjs marble testing with jasmine-marbles

2020-08-09
javascript/html/css

What is marble diagram?

marble-merge

마블 다이어그램은 위 사진처럼 일련의 시간 흐름에서 발생(emit)되는 값을 조각으로 명시하는 것이다. 어떠한 sequence 중에서 일정 부분을 포착해놓은 것이라고 할 수 있다. 위 예시는 두 흐름 시퀀스를 merge operator를 통해 합친 것이다. 그래서 테스트하기가 까다로울 수 있는데, 이를 일련의 규칙을 통해 테스트하는 것이 marble testing이다.

이때 흘러가는 각각의 프레임은 10ms로 본다. 예를 들어, ---o|라는 콜드 옵저버블은 30ms 후에 o라는 값을 방출하고 종료한다. 이렇게 마블 다이어그램을 string 형태로 표현해서 이를 ASCII marble diagrams이라고도 한다.


Marble Syntax


Example

(1) source 옵저버블이 일어날 때마다 startWith로 최초 시작한 후 항상 ‘done’이라는 값을 방출하는 옵저버블 테스트

const source$ = cold("-a-b-|");
const expected$ = cold("ab-c-|", {a: "done", b: "done", c: "done"});
const result$ = source$.pipe(
  startWith({}),
  switchMap(() => of("done"))
);

expect(result$).toBeObservable(expected$);


(2) hot 옵저버블에 대한 기본적인 테스트 (구독한 후 a를 방출하고 10ms 후에 종료)

const source = hot("-^a-|", {a: 5});
const expected = cold("-a-|", {a: 5});

expect(source).toBeObservable(expected);


(3) 비동기 상황을 동기적으로 테스팅하는 방법

HTML attribute vs DOM properties

2023-05-24
javascript/html/css

Typescript mixins in Angular

2022-01-16
javascript/html/css

Closure, especially in javascript

2020-04-18
javascript/html/css
comments powered by Disqus