Programming/SmallTalk
RxJS Marble Testing + Jest + NgRx Effect
HanDDol
2020. 6. 3. 13:32
m: expect.objectContaining({ type: ServerActions.InitialServersLoaded.type })
이 조합으로 검증을 하면 귀찮은 일이 많이 생긴다. 아래 같이 InitialServerLoaded Action을 발산할 때 Action에 Field가 여러 개고 랜덤으로 생성되는 값이면 아주 귀찮아진다.
it('무조건, IntialServersLoadedOk를 발산해야 한다', () => {
jest.spyOn(metricsApiSvc, 'getServerMetric')
.mockImplementation(() => of( mockResponse ));
testScheduler.run(helpers => {
const { cold, hot, expectObservable, expectSubscriptions, flush } = helpers;
actions$ = hot('-a----a-', { a: ServerActions.InitialServersRequesting() });
expectObservable(spectator.service.initialServersRequesting$).toBe(
'-(ml)-l-',
{
l: ServerActions.InitialServersLoadedOk(),
m: ServerActions.InitialServersLoaded({servers: {
id: 355232,
desc: 'desc'
})
}
);
})
});
RxJs Marble Testing를 Jest와 같이 사용하게 되면, Assert에 toEqual이 사용된다. 그리고 Jest에서는 다행히도 toEqual 내에서 사용 가능한 Matcher가 존재한다. 그중 하나가 expect.any이다. 그래서 다음과 같이 변경 가능하다.
// ...
m: ServerActions.InitialServersLoaded({servers: {
id: expect.any(number),
desc: expect.any(string)
})
// ...
그런데 이것도 필드 수가 많아지면 피곤해진다. type만 비교하고 싶은데 말이지. 그럴 때는 다음과 같이..
m: expect.objectContaining({ type: ServerActions.InitialServersLoaded.type })