Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork6.6k
feat: support import.meta.dirname and import.meta.filename#14854
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
feat: support import.meta.dirname and import.meta.filename#14854
Uh oh!
There was an error while loading.Please reload this page.
Conversation
linux-foundation-easyclabot commentedJan 15, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
netlifybot commentedJan 15, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview forjestjs ready!Builtwithout sensitive environment variables
To edit notification comments on pull requests, go to yourNetlify site configuration. |
| } | ||
| const[path,query]=specifier.split('?'); | ||
| const[specifierPath,query]=specifier.split('?'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NOTE: Conflicting name with importednode:path.
packages/jest-runtime/src/index.ts Outdated
| initializeImportMeta:(meta:JestImportMeta)=>{ | ||
| meta.url=pathToFileURL(modulePath).href; | ||
| if(meta.url.startsWith('file://')){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NOTE: The check here is because of the CAVEAT mention in nodejs docs (seehttps://nodejs.org/dist/latest-v20.x/docs/api/esm.html#importmetadirname)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
in here there'll always be afile:// protocol, I think? Due to thepathToFileURL on line 521
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thats a good point, let me update it.
4c44b56 to7abba69Compareb37f48d tod04fb81Compare| meta.url=pathToFileURL(modulePath).href; | ||
| //@ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/node@20.11.0 | ||
| meta.filename=fileURLToPath(meta.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
should we only add these if the underlying Node version supports them? That way the tests won't behave differently than at runtime if people rely on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would say it should not be necessary as users on node < 20.11 won't use those variables in code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@SimenB Do you want to add the check? I would backport this change also to v29 when it is ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Do you want to add the check?
nah, I'm fine with it. If a feature check were easy it'd be fine, but it's a bit clunky, and I don't wanna check against specific node versions.
I would backport this change also to v29 when it is ready.
We don't do backports, so there's no need for that. Thanks for offering, though!
SimenB commentedJan 15, 2024
New test is failing on windows. probably need a |
alesmenzel commentedJan 15, 2024
Right, that will be the opposite slash probably. |
| expect( | ||
| import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'), | ||
| ).toBe(true); | ||
| if(process.platform==='win32'){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
is this correct? If it's afile:// thing, it should use forward slashes. See examples inhttps://nodejs.org/api/url.html#urlpathtofileurlpath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don't have access to a windows PC, or this'd be easy to check 😅
@G-Rath would you be able to quickly check for us whatimport.meta.filename andimport.meta.dirname returns on a windows machine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Testing with an arbitrary file on v21 gives me:
// file.jsimport { fileURLToPath } from 'node:url';console.log(fileURLToPath(import.meta.url));console.log(import.meta.filename);console.log(import.meta.url);C:\Users\G-Rath\workspace\projects-oss\jest\file.mjsC:\Users\G-Rath\workspace\projects-oss\jest\file.mjsfile:///C:/Users/G-Rath/workspace/projects-oss/jest/file.mjsLet me know if that's enough or if you'd like me to try actually running this test (or other arbitrary bits of code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@SimenB filename and dirname are OS specific, but meta.url has URL format with forward slashes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Perfect, thanks@G-Rath!
SimenB left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
thanks!
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. |
Summary
Make jest compatible withNode 20.11.0 and above running ESM code.
Adds support for
Test plan
See added test case.