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.

After switching from jasmine-marbles to jest-marbles jest spyOn stopped to work

See original GitHub issue

After switching from jasmine-marbles to jest-marbles jest spyOn stopped to work:

https://stackoverflow.com/questions/55705040/marble-test-fails-with-jest-but-equivalent-test-succeeds-with-jasmine

With Jasmine is runs successful:

import { JasmineMarble } from './jasmine-marble';
import { cold } from 'jasmine-marbles';
import { switchMap } from 'rxjs/operators';
import { EMPTY, Observable } from 'rxjs';

class Service {
  foo(): Observable<any> {
    return EMPTY;
  }

  bar(a): Observable<any> {
    return EMPTY;
  }
}

describe('JasmineMarble', () => {
  it('should create an instance', () => {
    const service = new Service();

    spyOn(service, 'foo').and.returnValue(cold('a|', { a: 'A' }));
    spyOn(service, 'bar').and.returnValue(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));

    const result$ = service.foo().pipe(switchMap(a => service.bar(a)));

    expect(result$).toBeObservable(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
    expect(service.bar).toHaveBeenCalledWith('A');
  });
});

With Jest it fails with this error trace:

expect(jest.fn()).toHaveBeenCalledWith(expected)

Expected mock function to have been called with:
  ["A"]
But it was not called.

The Jest code:

import { JestMarble } from './jest-marble';
import { cold } from 'jest-marbles';
import { switchMap } from 'rxjs/operators';
import { EMPTY, Observable } from 'rxjs';

class Service {
  foo(): Observable<any> {
    return EMPTY;
  }

  bar(a): Observable<any> {
    return EMPTY;
  }
}

describe('JestMarble', () => {
  it('should create an instance', () => {
    const service = new Service();

    jest.spyOn(service, 'foo').mockReturnValue(cold('a|', { a: 'A' }));
    jest.spyOn(service, 'bar').mockReturnValue(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));

    const result$ = service.foo().pipe(switchMap(a => service.bar(a)));

    expect(result$).toBeObservable(cold('a-b-c|', { a: 'A', b: 'B', c: 'C'}));
    expect(service.bar).toHaveBeenCalledWith('A');
  });
});

You can find an example repository here: https://github.com/stijnvn/marbles The Jasmine example can be run with ng test jasmine-marbles. The Jest one with ng test jest-marbles.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
just-jebcommented, Sep 7, 2019

Please use toSatisfyOnFlush, available since 2.5.0.
Let me know if it solves your problem and the issue can be closed.

2reactions
zoechicommented, Feb 13, 2021

toSatisfyOnFlush solved it for me but it took several hours until I figured out why toHaveBeenCalled...() didn’t work. The README.md mentions it, but it took a while until I realized that this is related.

Read more comments on GitHub >

github_iconTop Results From Across the Web

After switching from jasmine-marbles to jest-marbles ... - GitHub
After switching from jasmine-marbles to jest-marbles jest spyOn stopped to work : ... Please use toSatisfyOnFlush , available since 2.5.0.
Read more >
Marble test fails with Jest, but equivalent test succeeds with ...
Same for me - after switching from "jasmine-marbles": "=0.3.1" to "jest-marbles": "=2.3.1" , all tests with spies started to fail :(.
Read more >
jest-marbles - npm
Marble testing helpers library for RxJs and Jest. ... Start using jest-marbles in your project by running `npm i jest-marbles`.
Read more >
Unit Testing NGRX RxJS with Marbles - Sam Brennan & Keith ...
ng-conf is a two day, single track conference focused on delivering the highest quality training in the Angular JavaScript framework.
Read more >
How to test Observables. The ultimate guide - Medium
In this blog post, we are going to see the two different strategies on how to test an Observable. The “subscribe and assert...
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 Hashnode Post

No results found