From 2a29e92559e3ecccb212db19b01caef1a810faa2 Mon Sep 17 00:00:00 2001 From: Shuqian Hon Date: Thu, 31 May 2018 22:49:27 +1000 Subject: [PATCH] feat(output): pretty print marble differences for value objects --- spec/to-be-observable.spec.ts | 8 ++++++++ src/jest/custom-matchers.ts | 11 ++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/spec/to-be-observable.spec.ts b/spec/to-be-observable.spec.ts index 813ba75..1f6eb67 100644 --- a/spec/to-be-observable.spec.ts +++ b/spec/to-be-observable.spec.ts @@ -11,6 +11,14 @@ describe('toBeObservable matcher test', () => { expect(a$.pipe(concat(b$))).toBeObservable(expected); }); + it('Should work for value objects', () => { + const valueObject = {foo: 'bar'}; + const a$ = cold('-a-|', {a: valueObject}); + const expected = cold('-a-|', {a: valueObject}); + + expect(a$).toBeObservable(expected); + }); + it('Should merge two hot observables and start emitting from the subscription point', () => { const e1 = hot('----a--^--b-------c--|', ); const e2 = hot( '---d-^--e---------f-----|', ); diff --git a/src/jest/custom-matchers.ts b/src/jest/custom-matchers.ts index e406807..7e50826 100644 --- a/src/jest/custom-matchers.ts +++ b/src/jest/custom-matchers.ts @@ -1,5 +1,6 @@ import { Marblizer } from '../marblizer'; import diff from 'jest-diff'; +import equal from 'fast-deep-equal'; import { printExpected, printReceived, matcherHint } from 'jest-matcher-utils'; import { TestMessage } from 'rxjs/testing/TestMessage'; import { SubscriptionLog } from 'rxjs/testing/SubscriptionLog'; @@ -13,18 +14,18 @@ function haveValueObjects(actual: TestMessage[], expected: TestMessage[]) { export const customTestMatchers = { toBeNotifications(actual: TestMessage[], expected: TestMessage[]) { - let actualMarble: string; - let expectedMarble: string; + let actualMarble: string | TestMessage[]; + let expectedMarble: string | TestMessage[]; if (haveValueObjects(actual, expected)) { - actualMarble = JSON.stringify(actual); - expectedMarble = JSON.stringify(expected); + actualMarble = actual; + expectedMarble = expected; } else { actualMarble = Marblizer.marblize(actual); expectedMarble = Marblizer.marblize(expected); } - const pass = actualMarble === expectedMarble; + const pass = equal(actualMarble, expectedMarble); const message = pass ? () =>