Skip to content

Instantly share code, notes, and snippets.

@kylewhitaker
Last active September 9, 2017 00:34
Show Gist options
  • Save kylewhitaker/a2aa141a318c2743d5a0440bf5cf2bba to your computer and use it in GitHub Desktop.
Save kylewhitaker/a2aa141a318c2743d5a0440bf5cf2bba to your computer and use it in GitHub Desktop.
Synchronously unit testing asynchronous functions can produce false positive passing tests!
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
beforeEach(() => {
component = new SomeComponent();
});
describe('returnSomeObservable', () => {
// THIS TEST SHOULD FAIL, BUT IT PASSES!
it('should return an observable that emits a \'test\' string value', () => {
component.returnSomeObservable().subscribe((value) => {
expect(value).not.toEqual('test'); // not executed until Observable emits a value
});
// When done() is not explictly defined, the test will complete here.
});
});
});
import { Observable } from 'rxjs';
export class SomeComponent {
returnSomeObservable(): Observable<string> {
return Observable.of('test').delay(3000); // delay emissions by 3 seconds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment