-
Notifications
You must be signed in to change notification settings - Fork 31.3k
Test runner module mocking format #53634
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
Comments
I think the simplest way to solve this would be to use the hint if present. If the hint is not present, use some default. To be more powerful/flexible, we could add another option to the API that lets the user be explicit about the format they want. |
What does this mean in practice? If I think rather than having the docs explain how the hint is determined based on URL and when it might not exist and what happens in that case, et cetera, a simpler approach could be explained in the docs as follows:
|
I would avoid using this heuristic because How many cases in the current module mocking tests begin to fail with the changes in #53619? If it is a lot, then I think we may want to figure out an appropriate default (maybe |
In
Basically any test where the mock URL is referring to a Since there’s no file at the URL provided, I’m not sure why we’re using anything about the URL as part of determining format. That part feels wrong to me.
Because of the time when they can be used together, if the default export happens to be an object with properties that match the provided named exports? It seems to me like the most convenient API would be one where the sources are always generated as ESM and |
Actually I might’ve been mistaken about one part: does the URL of the module to be mocked need to exist? Because if we only support mocking modules that exist, we can use |
It seems like @cjihrig above is referring to this? https://nodejs.org/docs/latest/api/test.html#mockmodulespecifier-options. Which seems to be implemented here?
node/lib/internal/test_runner/mock/mock.js
Lines 489 to 516 in 02bd866
Per the docs,
mock.module
supports defining mocks for both module systems based on passed-in objects (not source code). Then we generate source code here?node/lib/test/mock_loader.js
Lines 163 to 175 in 02bd866
At first I was confused as to how this works, since ESM is unlike CommonJS in that ESM can have a default export that’s something other than an object whose properties are the named exports; so how can this one API generate mocks that apply to both module systems while still supporting this ESM-only feature. But then I noticed that an exception gets thrown if the mock is created with both
defaultExport
andnamedExports
defined and the module is required. So I guess really it supports both module systems only if you give it input that can be consumed by both systems.But maybe that’s the answer: if the user defines one of
defaultExport
ornamedExports
, it creates the source as CommonJS, and either maps the provideddefaultExport
tomodule.exports
or it adds each of thenamedExports
ontomodule.exports
. If the user defines bothdefaultExport
andnamedExports
, the mock is created as ESM and the restrictions mentioned in the docs apply.Alternatively the mock could always be created as ESM, though then you have the issue that for it to be
require
-able--experimental-require-module
needs to be enabled. But maybe that behavior could be opted into by using this API. Since the user is passing in an object I assume we don’t need to worry about top-levelawait
.Or we could just ask the user what format to generate the source code in. We kind of already are, though the users probably don’t realize it, since we ask for the URL of the module to be mocked and base the format on that. Though it seems like you’re trying to keep this as simple as possible so if there’s a way to avoid adding another option, I assume you’d prefer to determine this implicitly. I think determining format based on
defaultExport
/namedExports
is a better source to use rather than how the file URL resolves.The text was updated successfully, but these errors were encountered: