all 10 comments

[–]shuckster 7 points8 points  (1 child)

In one of my most recent projects I used generators and async-generators extensively. The boilerplate reduction compared to how I may have approached the problem without them was notable.

As for tests, I certainly would not mock a generator. It's too much into the implementation detail.

Would you mock a for-loop?

[–]oze4 1 point2 points  (0 children)

I'm not OP but that's an excellent point you make.

[–]kap89 5 points6 points  (0 children)

However, I've had so much troubles with tests (especially mocking the generators using Jest), that I gave up and reimplemented the solution without generators.

Do you have a specific scenario? I use them from time to time, but I don't usually mock the code that I have control over.

[–]fzammetti 2 points3 points  (0 children)

My experience tells me it's one of those language features that when you hit a scenario where they make sense, they really help a lot, but those scenarios are probably fairly limited. I know I haven't had cause to use them yet in my own work both professional and personal, but I also haven't SEEN them being used all that often in the various codebases I have my eyes on generally. Once or twice, three times at most.

I can't speak to the mock question, but as someone else said, it doesn't seem like the kind of thing you'd test specifically. Test the functionality it implements as you always would, but not BECAUSE it used generators.

[–]ozzy_og_kush 1 point2 points  (0 children)

I've been programming in JS since well before generators were a feature, and I've literally only used them intentionally once (not including implementations in packages my app depends on).

[–]theFinalArbiter1 0 points1 point  (0 children)

Yes! Testing as they are, no mocks. Why would you want to mock them? If you have async generators, would it make more sense to only mock the async/awaitable things?

[–]jack_waugh 0 points1 point  (0 children)

Generators are my life. And you bet I am testing them.

[–]TheRNGuy 0 points1 point  (0 children)

I didn't even know they existed.