Skip to content

Instantly share code, notes, and snippets.

@Tallyb
Last active May 3, 2018 04:55
Show Gist options
  • Save Tallyb/2f21e4c166f4352a7c1fde7c2df4e112 to your computer and use it in GitHub Desktop.
Save Tallyb/2f21e4c166f4352a7c1fde7c2df4e112 to your computer and use it in GitHub Desktop.
Observable error problem
//update: Adding async on the subscribe does the trick...
it('should call observable', () => {
let mockFn = jest.fn();
mockFn.mockImplementation(() => {
return of(4)
});
mockFn().subscribe(async (res) => {
console.log('RES', res); // it logs 4
await expect(res).toEqual(8);
});
});
//
//This one will pass
//
it('should call observable', () => {
let mockFn = jest.fn();
mockFn.mockImplementation(() => {
return of (4)
});
mockFn().subscribe(res => {
console.log('RES', res); // it logs 4
expect(res).toEqual(8);
});
});
//
//this one will fail correctly
//
it('should call observable', () => {
let result;
let mockFn = jest.fn();
mockFn.mockImplementation(() => {
return of (4)
});
console.log('I am before')
mockFn().subscribe(res => {
console.log('RES', res);
result = res;
});
expect(result).toEqual(8);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment