Last active
July 9, 2022 10:51
-
-
Save danillouz/b541a21e8981ae1cdc51b5f0c594105a to your computer and use it in GitHub Desktop.
Use generator functions in mocha js test suite methods.
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
/** | |
* Make sure the following is included in `/test/index.js` | |
* | |
* ```javascript | |
* 'use strict'; | |
* | |
* const mocha = require('mocha'); | |
* const coMocha = require('co-mocha'); | |
* | |
* coMocha(mocha); | |
* | |
* // require your test files here.. | |
* ``` | |
* | |
* After including this snippet, you can use generators in mocha test suite methods. | |
*/ | |
const chai = require('chai'); | |
const expect = chai.expect; | |
describe('Use generators', function () { | |
it('yield a Promise that resolves with a Number', function *() { | |
const value = yield Promise.resolve(8); | |
expect(value).to.equal(8); | |
}); | |
it('yield a Promise that rejects with an Error', function *() { | |
let value; | |
try { | |
value = yield Promise.reject(new Error('Kaput')); | |
} catch(err) { | |
expect(value).to.be.undefined; | |
expect(err.message).to.equal('Kaput'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment