Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing "it.yield", a convenient way to test promise code #9601

Merged
merged 4 commits into from
Jun 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions test/functional/test-yield.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as lolex from 'lolex';
import {macroTask} from '../../testing/yield';

describes.realWin('yield', {}, env => {

Expand All @@ -41,7 +42,7 @@ describes.realWin('yield', {}, env => {

nestPromise(100);
expect(value).to.be.false;
yield;
yield macroTask();
expect(value).to.be.true;
});

Expand All @@ -56,7 +57,7 @@ describes.realWin('yield', {}, env => {
value = true;
});
expect(value).to.be.undefined;
yield;
yield macroTask();
expect(value).to.be.true;
});

Expand All @@ -72,7 +73,7 @@ describes.realWin('yield', {}, env => {
expect(value).to.be.undefined;
clock.tick(100);
expect(value).to.be.false;
yield;
yield macroTask();
expect(value).to.be.true;
});

Expand All @@ -90,7 +91,7 @@ describes.realWin('yield', {}, env => {
}, 100);
clock.tick(100);
expect(value).to.be.false;
yield;
yield macroTask();
expect(value).to.be.true;
});

Expand All @@ -101,4 +102,14 @@ describes.realWin('yield', {}, env => {
const result = yield promise;
expect(result).to.equal('yes');
});

it('should be able to expect throwable', function* () {
const promiseThatRejects = Promise.reject(new Error('OMG'));
try {
yield promiseThatRejects;
throw new Error('UNREACHABLE');
} catch (e) {
expect(e.message).to.contain('OMG');
}
});
});
53 changes: 29 additions & 24 deletions testing/yield.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

/**
* Install "yield" support to Mocha tests.
* Install "YieldIt" support to Mocha tests.
* "YieldIt" allows you to wait for a promise to resolve before resuming your
* test, so you can write asynchronous test in a synchronous way.
* Check test-yield.js for how-to.
*/
export function installYieldIt(realIt) {
Expand All @@ -24,44 +26,47 @@ export function installYieldIt(realIt) {
it.skip = realIt.skip;
}

/**
* A convenient method so you can flush the event queue by doing
* `yield macroTask()` in your test.
* @returns {Promise}
*/
export function macroTask() {
return new Promise(setTimeout);
}

function enableYield(fn, message, runnable) {
if (!runnable || !runnable.constructor
|| runnable.constructor.name !== 'GeneratorFunction') {
return fn(message, runnable);
}
return fn(message, done => {
const runner = (iterator, result) => {
const iterator = runnable();
function step(method, result) {
let state;
try {
state = iterator.next(result);
state = iterator[method](result);
} catch (e) {
// catch any assertion errors and pass to `done`
// otherwise the messages are swallowed
return done(e);
}
if (state.done) {
return done();
Promise.resolve(state.value).then(() => done(), _throw);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, the iterator is done and pushing the error into it won't be handled. This should be then(() => done(), done)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. fixed.

return;
}

const _runner = runner.bind(null, iterator);
if (isPromise(state.value)) {
// With this, we can do: `const result = yield promise;`
state.value.then(_runner).catch(done);
} else {
// With this, we can do: `yield 50;`, which blocks the test for 50ms
// We should rarely need this in unit test, use with caution, as it
// usually brings test flakiness.
const timeout = (typeof state.value === 'number') ? state.value : 0;
setTimeout(_runner, timeout);
}
};
runner(runnable());
});
}
Promise.resolve(state.value).then(_next, _throw);
}

function isPromise(subject) {
if (subject === undefined || subject === null) {
return false;
}
return typeof subject.then == 'function';
function _next(value) {
step('next', value);
}

function _throw(error) {
step('throw', error);
}

_next();
});
}