question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to perform checks after expect().toBeObservable() ?

See original GitHub issue

Back when I was using karma with jasmine-marbles I could write tests like this:

expect(result).toBeObservable(expected);
expect(mockInsideObservable).toHaveBeenCalled();

Which is easy to read and write. The main thing here is that expect.toBeObservable acts synchronously and waits for all the code inside the observable to be executed.

The same code in jest and jest-marbles will always fail with the following message:

Expected mock function to have been called, but it was not called

Because the last assertion is evaluated before the observable. Is this expected behavior? If so, would it be possible to provide some sort of promise or callback to the observable assertion?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
just-jebcommented, Sep 7, 2019

Please use toSatisfyOnFlush, available since 2.5.0.
Let me know if that helps.

1reaction
just-jebcommented, Jul 27, 2019

Currently it works in a way that the scheduler is being flashed after each test, therefore your observable is essentially invoked in afterEach.
I agree it should change and work in the same way jasmine-marbles work.
Until it is implemented please use the workaround provided above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test Observables. The ultimate guide - Medium
If the last item would be emitted after 20 seconds instead of 3? Our test would fail. So that's no solution. We don't...
Read more >
Testing Observables with jest - rxjs - Stack Overflow
it('should do the job', async () => { await expect(myObservable .pipe(first()) .toPromise()) .resolves.toEqual(yourExpectation); });.
Read more >
Testing Observables in Angular - Netanel Basal
When we run detectChanges() , the ngOnInit() hook will run and execute the subscription function synchronously, causing the isLoading property to always be ......
Read more >
How to Test Five Common NgRx Effect Patterns
initialiseComplete(), }); expect(effects.initialiseThing$).toBeObservable(expected); });. This shows ...
Read more >
NgRx Testing: Components | Brian F Love
toBeObservable (expected); }); });. We want to test both the dispatch() and pipe() methods that are invoked in the ngOnInit() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found