After switching from jasmine-marbles to jest-marbles jest spyOn stopped to work
See original GitHub issueAfter switching from jasmine-marbles to jest-marbles jest spyOn
stopped to work:
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:
- Created 4 years ago
- Reactions:1
- Comments:18 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Please use
toSatisfyOnFlush
, available since 2.5.0.Let me know if it solves your problem and the issue can be closed.
toSatisfyOnFlush
solved it for me but it took several hours until I figured out whytoHaveBeenCalled...()
didn’t work. The README.md mentions it, but it took a while until I realized that this is related.