m.cichacz
m.cichacz

Reputation: 759

RxJS marble testing objects with functions

I am returning from the Observable an object. One of its properties is a function.
After assigning even empty function and emitting the object the toBeObservable expectation fails because of non-deep match.

I'm using rxjs-marbles/jest for testing. Here's sample test case:

it('...', marbles(m => {
  const source = m.cold('(a|)');
  const expected = m.cold('(b|)', { b: {
    label: 'A',
    action: () => { }
  } });

  const destination = source.pipe(
    map(() => ({
      label: 'A',
      action: () => { }
    }))
  );
  m.expect(destination).toBeObservable(expected);
}));

The result is as follows:

expect(received).toEqual(expected) // deep equality

Expected: [{"frame": 0, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value": {"action": [Function action], "label": "A"}}}, {"frame": 0, "notification": {"error": undefined, "hasValue": false, "kind": "C", "value": undefined}}]
Received: serializes to the same string

The only thing I need to check is whether the action is defined in object. Is it possible?

Upvotes: 1

Views: 1257

Answers (1)

m.cichacz
m.cichacz

Reputation: 759

Turns out one can define expected like this:

const expected = m.cold('(b|)', { b: {
    label: 'A',
    action: expect.any(Function)
  } });

And the result will match.

Weird thing tho, I think I tried it few days ago and then it wouldn't work, but maybe I did it somehow different.

Upvotes: 2

Related Questions