javascript - How Do I Test nested ES6 Generators using Mocha? -
i'm trying use co-mocha test nested generators functionality in koa app. class works fine @ runtime, when attempt test functionality, cannot nested generator run in test.
class being tested:
import promise 'bluebird' class fooservice { _doasync(){ return new promise((resolve) => { settimeout(() => { resolve({ foo: 'foo' }) }, 500) }) } create(){ console.log('this never gets logged') let self = return function*(){ console.log(`this doesn't either`) return yield self._doasync() } } } export default new fooservice() test file
import fooservice '../services/foo-service' import chai 'chai' let expect = chai.expect describe('testing generators', () => { it('should work', function *(){ console.log('this log never happens') let result = yield fooservice.create() expect(result).to.equal({foo: 'foo'}) }) }) i'm running mocha w/ --require co-mocha , node 4.2.6
while tests complete w/o errors, none of console above ever logged , i'm quite sure actual test generator never running @ all.
if try using npm package, mocha-generators instead, while log inside test generator, underlying generator returned create() method on service never fires...
what doing wrong??
without mocha-generators, it callback returns generator not run anyone. you'd need wrap in co manually mocha receive promise.
with mocha-generators, generator executed yields generator function. that's not expected, supposed yield promises. need call generator function create() call returns yourself, , shouldn't yield generator rather delegate via yield*:
let result = yield* fooservice.create()();
Comments
Post a Comment