Skip to content
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

[wip] feat(@jest/environment, jest-resolve, jest-runtime): support URL as module specifier in mocking APIs #14332

Closed

Conversation

mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Jul 15, 2023

Fixes #14040
Closes #14297

Summary

The idea is to support URL strings and URL objects as module specifiers in mocking APIs. Recent versions of Node allow URLs almost everywhere where module specifier can be passed. So this feels like a natural step forward. For the beginning I think it is better to focus on mocking APIs only, altho there are other APIs in Jest packages which could take URL.

This PR is based on the ideas I came up while reviewing #14297. I think the problem is not the node: schema, but the fact that jest-resolve was not made to support URL. That is relatively new feature in Node.

For example, currently Jest logic treats node:fs and fs as two separate modules. Adding support for URL specifiers this gets solved. That is not just about stripping node:, but about supporting URL strings.

This also can be useful for ESM where import.meta.url is an URL string. I don’t have any use case, but in theory dynamic mocking and importing based on import.meta.url could be implemented.

Test plan

  • current tests are passing
  • add units tests
  • add e2e tests
  • add type tests
  • documentation

@netlify
Copy link

netlify bot commented Jul 15, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 83f5041
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/64ba5c4df3dc7c000852ca81
😎 Deploy Preview https://deploy-preview-14332--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@SimenB
Copy link
Member

SimenB commented Jul 21, 2023

Yeah, this would be awesome! 👍

@mrazauskas
Copy link
Contributor Author

mrazauskas commented Jul 21, 2023

Thanks for encouragement. I just think my current approach is wrong: ignoring schemes is not the same as to support them. Would be better to use URL strings as module identifiers instead of current module ID logic. That is more or less what Node does in with EMS modules. Worth to give a try.

Also it would be too much to have support Node v14. So this can land only with Jest 30. I can think slowly.

@SimenB
Copy link
Member

SimenB commented Jul 21, 2023

Using strings as identifiers under the hood sounds like a good idea 👍

@mrazauskas
Copy link
Contributor Author

This one is rather involving. Can find spare time at the moment.

@mrazauskas mrazauskas closed this Nov 23, 2023
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Manual mocks file name for node protocol imports (node:)
2 participants