-
Notifications
You must be signed in to change notification settings - Fork 542
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
Add an ability to clear pending interceptors in MockAgent
#3737
Comments
As far as I understand the code, the implementation would be quite straightforward: I need to clean the |
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
Sure, I will give it a try |
Not to disregard, I agree the feature might be beneficial; but I wouldn't discard this option as adds the most isolation to your tests cases, reducing the surface of side-effects between them than can lead to flaky or weak tests. |
@metcoder95 thanks for your answer!
For context, I use |
Yes.
I don't think so |
@mcollina thanks for the answer! |
Agreed! |
@stanislav-halyn I came across the same issue and I ended up writing this into my test suite setup export const TestSuiteMockAgent = new MockAgent();
export const clearInterceptors = () => {
if (!TestSuiteMockAgent.pendingInterceptors()?.length) return;
const kClients = Object.getOwnPropertySymbols(TestSuiteMockAgent).find((s) => s.toString() === "Symbol(clients)");
if (!kClients) return;
// @ts-ignore
const clients = [...TestSuiteMockAgent[kClients].values()];
for (const client of clients) {
const kDispatches = Object.getOwnPropertySymbols(client).find((s) => s.toString() === "Symbol(dispatches)");
client[kDispatches] = [];
}
}; I'm happy to open a PR soon and extend the mockAgent. I'm basically clearing the Potentially we could also add a selector as an argument to pick which interceptors to clear. can be useful in some sophisticated testing scenarios |
This would solve...
I'm migrating from
nock
toundici
mocks and after each test, I clear all active mocks, so that they do not interact with other test cases. This solves the problem when mocks from a failed test might interact with other test cases, making debugging difficult.The implementation should look like...
It would be great to be able to call a
cleanMocks
method onMockPool
orMockAgent
to clean all pending mocks.I have also considered...
I have considered creating a separate
MockAgent
instance for every test case, but I'm not sure it's a good idea since it can have a performance overhead and a lof of boilerplate code for cleaning up the mock agent.I also considered using
.assertNoPendingInterceptors()
after each test case, but it leads to all tests failing if any of them haven't used a mocked request.Additional context
It's the same feature nock has.
The text was updated successfully, but these errors were encountered: