Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

@alesmenzel
Copy link
Contributor

Summary

Make jest compatible withNode 20.11.0 and above running ESM code.
Adds support for

import.meta.filenameimport.meta.dirname

Test plan

See added test case.

@linux-foundation-easycla
Copy link

linux-foundation-easyclabot commentedJan 15, 2024
edited
Loading

CLA Signed

The committers listed above are authorized under a signed CLA.

@netlify
Copy link

netlifybot commentedJan 15, 2024
edited
Loading

Deploy Preview forjestjs ready!

Builtwithout sensitive environment variables

NameLink
🔨 Latest commit0c19357
🔍 Latest deploy loghttps://app.netlify.com/sites/jestjs/deploys/65a709227cbdac0008f3fff8
😎 Deploy Previewhttps://deploy-preview-14854--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 yourNetlify site configuration.

}

const[path,query]=specifier.split('?');
const[specifierPath,query]=specifier.split('?');
Copy link
ContributorAuthor

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.

SimenB reacted with thumbs up emoji
initializeImportMeta:(meta:JestImportMeta)=>{
meta.url=pathToFileURL(modulePath).href;

if(meta.url.startsWith('file://')){
Copy link
ContributorAuthor

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)

Copy link
Member

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

Copy link
ContributorAuthor

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.

@alesmenzelalesmenzelforce-pushed theam/add-import-meta-dirname-and-filename branch from4c44b56 to7abba69CompareJanuary 15, 2024 11:17
@alesmenzelalesmenzelforce-pushed theam/add-import-meta-dirname-and-filename branch fromb37f48d tod04fb81CompareJanuary 15, 2024 14:53
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);
Copy link
Member

@SimenBSimenBJan 15, 2024
edited
Loading

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

Copy link
ContributorAuthor

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.

Copy link
ContributorAuthor

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.

Copy link
Member

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
Copy link
Member

New test is failing on windows. probably need aslash or something. We should figure out what the value is on windows platforms, tho

@alesmenzel
Copy link
ContributorAuthor

New test is failing on windows. probably need aslash or something. We should figure out what the value is on windows platforms, tho

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'){
Copy link
Member

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

Copy link
Member

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?

Copy link
Contributor

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.mjs

Let me know if that's enough or if you'd like me to try actually running this test (or other arbitrary bits of code)

SimenB reacted with heart emoji
Copy link
ContributorAuthor

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

Copy link
Member

Choose a reason for hiding this comment

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

Perfect, thanks@G-Rath!

Copy link
Member

@SimenBSimenB left a comment

Choose a reason for hiding this comment

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

thanks!

@SimenBSimenB merged commitbdb86aa intojestjs:mainJan 16, 2024
@github-actions
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 usingStackOverflow or ourdiscord channel for questions.

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsFeb 16, 2024
@SimenB
Copy link
Member

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@SimenBSimenBSimenB approved these changes

+1 more reviewer

@G-RathG-RathG-Rath left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@alesmenzel@SimenB@G-Rath

[8]ページ先頭

©2009-2025 Movatter.jp