Skip to content

doc: call out potentially unexpected results when mocking and testing synchronous callback-based APIs #58170

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

filmaj
Copy link

@filmaj filmaj commented May 4, 2025

This PR fixes #58161.

When mocking synchronous callback-based APIs, the MockFunctionContext tracking mock calls won't be register until after the mock function finishes execution. Tests exercising callback-based code will likely not be written in a way to ensure this is the case. As such, call this out in the documentation.

When mocking synchronous callback-based APIs, the `MockFunctionContext`
tracking mock calls won't be register until after the mock function
finishes execution. Tests exercising callback-based code will likely
not be written in a way to ensure this is the case. As such, call this
out in the documentation.

Fixes: nodejs#58161
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. test_runner Issues and PRs related to the test runner subsystem. labels May 4, 2025
@@ -652,6 +652,86 @@ test('spies on an object method', (t) => {
});
```

### Mocking callback-based APIs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should document this as specific to callback based APIs. It probably makes sense to call them out as a case where you can encounter this issue, but it doesn't inherently have anything to do with callbacks. The problem also goes away when making callbacks asynchronous, as they generally are within Node.

The issue is really that the call is not recorded until the mocked function returns. You would run into the same issue if you tried to access the call from within a mock of a synchronous functon.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I struggled with how to frame this particular situation in the context of these docs; I think it deserves call-out status in the docs, but I am also biased to think that given I spent some time digging into this issue to figure out what was going on 😛

You would run into the same issue if you tried to access the call from within a mock of a synchronous function.

Is this a reasonable equivalency? From my own perspective, I wouldn't expect to be able to access MockFunctionContext references as the mock itself is still executing. But I did expect to be able to do so when mocking a sync callback. Perhaps this reveals more about my lack of understanding of node internals more than anything else 😆 but I also see this discussion as the magic that is consumers of a project hashing out some point with maintainers of a project - we each bring our own biases to bear but hopefully can meet in the middle somewhere.

I'll have a think about how to frame this gotcha as the result of the mock still executing rather than something unique to callback-based APIs. Open to suggestions / ideas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MockFunctionContext cannot be used to track callback / continuation-passing methods
3 participants