testdouble.js
testdouble.js copied to clipboard
td.when breaks after td.replace on objects methods generators
Possibly related to #271.
const Service = {
*send () {},
}
td.replace(Service, 'send');
td.when(Service.send()).thenResolve('fake');
Running example: https://runkit.com/albertogasparin/595b9e9a08f4860012b4af3e It works fine in pre 3.2
Pre 3.2, generators were naively replaced like any other function, but they don't really behave like regular functions, so 3.2.2 formally drops support (it no longer attempts to replace them until such a time as we can provide proper, well-thought-out support.
I'm not sure how valid your example would be for actually specifying real-world behavior of a generator function. Could you expand on it to show how the pre-3.2 behavior was providing real value to you? I don't have much experience with generators, but I can't see it.
Tracking the enhancment in #276
The reason why I'm using generators is because I'm stuck with Koa 1.x (Koa v2 uses async/await but requires node > 7.6 while I'm still on node v6). In Koa I can use them as control flow (just like async/await) thanks to the co library. So I can write any code as it is syncronous:
function* myAsyncFn () {
let data = yield Promise.resolve('ok'); // yeld supports generators, promises, callbacks, arrays, ...
return data;
}
As I'm in a generators world, I'm using them for nearly all modules with async operations:
function* getData () {
const data = yield myAsyncFn();
console.log(data);
}
// you can use co to try it out
co(getData);
So td pre v3.2 was working because I just need to replace myAsyncFn with a function that returns an acceptable value for yield (such a promise).