[RFC] setupPolly method for jest
See original GitHub issueHi team! First of all, thanks a lot for this library, great work 👏
This issue is a proposal and request for comments on adding a setupPolly
helper for jest environments. After having a discussion with @offirgolan in https://github.com/Netflix/pollyjs/pull/115#discussion_r222127410 I decided to investigate is it possible to make the same hook support with automated name generation for jest/jasmine environments as the one implemented for Mocha and QUnit in the core of this library.
There are two main differences that I spotted between those two mentioned above and jest/jasmine:
this
is not commonly used in jest, so access to polly instance should be handled differently- there is no “normal” way to acces the current test name in hooks
Trying to address those issues led me to the solution that can be considered hacky, so instead of going with the PR right away, I created setup-polly-jest
library as a proof of concept (but it works and covered with tests)
During my investigation I found out that you can get access to the full test name, but to do this, you’ll need a return value from {test,it}
call in the test suite.
The solution is to proxy jasmine.Env.it
and jasmine.Env.dit
methods to get the access to test case instance, get the full name and also hook into before and after hooks of the test case, overriding another jasmine method.
To address the this
issue I decided on a little bit different approach, where setupPolly
will return context
with polly instance as a property (as a bonus, it will also have clearPolly
method to disable polly at any moment in the test workflow)
You can find repository here: https://github.com/gribnoysup/setup-polly-jest and it is even published as @gribnoysup/setup-polly-jest
so it should be easy for you to test how it works. Repo contains documentation, unit tests and an “integration” test suite that uses the library itself to setup polly in jest test environment
If you think that this approach is acceptable, I’ll be glad to integrate this solution into @pollyjs/core
package 👍
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:8 (5 by maintainers)
I updated the repo and now everything is working both for jasmine and jest-jasmine and also matching Mocha/QUnit as close as possible: scoping, naming with folders, setupPolly method API 👍
@offirgolan if you are still okay with the approach, I’m still happy to port it to
@pollyjs/core
😸@gribnoysup apologies for the delayed response on this! Thank you again for answering all of my concerns and for taking the time to investigate the jest/jasmine issues further. Looking at the repo, I think this is well executed and would be a super useful addition to the library. Feel free to open up a PR into
@pollyjs/core
with this and I’ll do whatever I can to help you get this in asap 😄