Skip to content

Instantly share code, notes, and snippets.

@danillouz
Last active July 9, 2022 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danillouz/b541a21e8981ae1cdc51b5f0c594105a to your computer and use it in GitHub Desktop.
Save danillouz/b541a21e8981ae1cdc51b5f0c594105a to your computer and use it in GitHub Desktop.
Use generator functions in mocha js test suite methods.
/**
* 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