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

td.when breaks after td.replace on objects methods generators #275

Open
albertogasparin opened this issue Jul 4, 2017 · 3 comments
Open

Comments

@albertogasparin
Copy link

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

@searls
Copy link
Member

searls commented Jul 5, 2017

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.

@searls
Copy link
Member

searls commented Jul 5, 2017

Tracking the enhancment in #276

@albertogasparin
Copy link
Author

albertogasparin commented Jul 5, 2017

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).

@jasonkarns jasonkarns added this to Backlog in Test Double Trouble Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants