jasmine-co is a simple Jasmine 2.x adapter that allows you to use
co and ES6 generator functions to greatly
simplify your asynchronous test code using synchronous patterns.
jasmine-co also enables you to return promises from your specs without
manually worrying about handling Jasmine's done callback. For you
TypeScript fans, this means you can trivially use async/await.
Testing asynchronous functions doesn't have to be painful.
Requirements
NodeJS with support for generators
you can use nodejs@0.12.x with --harmony
or save yourself some trouble and just use nodejs@4.x which enables
support for generators, arrow functions, and other ES6 features by
default
Jasmine 2.x
Quick Start
Install jasmine-co
globally, e.g. in a helpers file
install / uninstall within a specific describe block
install / uninstall for a specific it
one-off usage
etc.
Write tests as normal, but instead of using function + done, either...
use function* and yield, or
a function that returns a promise (thennable)
That's it.
Installed globally
// spec/helpers/jasmine-co.helper.js
require('jasmine-co').install();
// spec/bookService.spec.js
describe("user models",function(){
beforeEach(function*(){
this.user=yieldgetUser(1);
});
it("should be able to get a list of owned books",function*(){
var books =yieldbookService.getBooksForUser(this.user);
expect(books).toEqual(jasmine.any(Array));
});
it("should also work when promises are returned",function(){