Last active
September 9, 2017 00:34
-
-
Save kylewhitaker/a2aa141a318c2743d5a0440bf5cf2bba to your computer and use it in GitHub Desktop.
Synchronously unit testing asynchronous functions can produce false positive passing tests!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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