-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Mock node modules with node: URL scheme #14297
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
… "node:" URL paths (jestjs#14297) Allow modules imported and mocked with 'node:*' URLs to be manually mocked by resolving just the path when generating module ID's for setting and checking mocks, and when resolving module names when importing mocks.
00e7b36
to
e53bc4f
Compare
… "node:" URL paths (jestjs#14297) Allow modules imported and mocked with 'node:*' URLs to be manually mocked by resolving just the path when generating module ID's for setting and checking mocks, and when resolving module names when importing mocks.
e53bc4f
to
f9aa9a5
Compare
… "node:" URL paths (jestjs#14297) Allow modules imported and mocked with 'node:*' URLs to be manually mocked by resolving just the path when generating module ID's for setting and checking mocks, and when resolving module names when importing mocks.
f9aa9a5
to
6a11f1e
Compare
Did you check if I found a branch where I was play with this fix. Seemed like it was enough to strip |
I strip The two spots where I strip The last change was editing |
@mrazauskas I hope I answered your questions well enough. Is there anything more I should do on this PR? |
Thanks for the details and for reminder. I was trying to look from the distance and to think a bit. These lines made me think: jest/packages/jest-runtime/src/index.ts Lines 1683 to 1687 in 70b17d2
Now strings are mostly normalized in What if we would normalize the user input as the first thing at all the entry spots? That would ensure that we pass clean up data to modules which already do the right job. |
By the way, you don’t have to force push. Commits are squashed when PRs are merged. |
I definitely see what you mean in doing it in one place and that would be definite improvement to the codebase. There's a few different methods that take a path as input that would need to changed, like Having the normalization in only the upper I was actually meaning to replace the snippet you linked with I think we should push this fix as is, after replacing that snippet, and then implement the more centralized and widely used normalizer in What do you think? |
Looks like I didn't forget 😆 6a11f1e#diff-c0d5b59e96fdc7ffc98405e8afb46d525505bc7b1c24916b5c8482de5a186c00L1707-L1712 |
You did not forget. This is why I saw these lines. Why not to implement the support in one go? Not sure why do you see this as major refactor? Just a line should be added for each method which take module name string. What is "centralized and widely used normalizer"? Is this those five lines of That was just an idea. Feel free rethink it or ignore it. Also worth mentioning that I don’t have rights to merge PRs. I can only leave comments here and there (; |
I was thinking of potential normalization for other cases than I gave this some more thought and I think both approaches have merits. If a dozen runtime methods pass to the resolver which normalizes in only two methods with only the snippet method being the outlier in the Also, I really appreciate your comments and insights, it's always good to be met with different approaches to consider and apply. |
@SimenB could you please approve/merge this PR? |
By the way, what happens if |
I was experimenting with somewhat bigger idea to add support for URL strings and URL objects. That would work for |
While I haven't seen URLs being used to import modules, if it works, then there's no reason not to support it fully. I you should pause until this PR is merged because of the refactors & corrections, and then expand the resolution logic it adds to account for URL's as you did. I personally feel like these additions could have been added to this PR given how similar the approaches are. But still, it's good to think of more general applications and consider the bigger picture, as well finding the most direct way to address issues. |
Hi @KhaledElmorsy @mrazauskas We're blocked on an ESM transition due to #14040 If we come up with a patch, would you like us to try and coordinate between this PR and #14332 or simply proceed independently? cc @virtuoushub |
Not sure I follow. How is the |
Not directly related to ESM, but as a precursor to ESM work, I recently opened a PR in
see also: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-node-protocol.md |
I am not sure if this relates but I am running into |
@tdeekens Hard to say. I don’t think manual mocks need |
This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
What's needed to move this PR along? |
This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
# Why In #14297, I used `node:` imports in `@expo/json-file`. Unfortunately, Jest doesn't pick this up when mocking `__mocks__/fs.ts`, it simply loads `node:fs` instead, see: jestjs/jest#14297 # How - Added `jest.mock('node:fs')` as workaround until Jest has something for `node:` mocking # Test Plan See CI # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [x] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin).
Summary
Allow modules that use
node:
paths, i.e.node:fs
, to be mocked and mapped to manual mocks appropriately.With Node adopting the aforementioned URL scheme, more users will migrate to it and expect their tools to support it.
Jest already incorporates checks for the URL scheme but not for mapping mocks and module names.
With this change, Jest strips the URL when resolving mocks and assigning
moduleID
s, making any of the following test setups in the next section valid and point to the same module.Source issue
Test plan
The change resolves the URL to the module name, so the following 3 e2e tests were added to verify that by checking if the
fs
module was mocked in the following 3 scenarios.Realistically, the first scenario is the target use case.