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

feat: support import.meta.dirname and import.meta.filename #14854

Merged

Conversation

alesmenzel
Copy link
Contributor

Summary

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

import.meta.filename
import.meta.dirname

Test plan

See added test case.

Copy link

linux-foundation-easycla bot commented Jan 15, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link

netlify bot commented Jan 15, 2024

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0c19357
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/65a709227cbdac0008f3fff8
😎 Deploy Preview https://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 your Netlify site configuration.

@@ -685,19 +697,22 @@ export default class Runtime {
specifier = fileURLToPath(specifier);
}

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

Choose a reason for hiding this comment

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

NOTE: Conflicting name with imported node:path.

@@ -520,6 +520,11 @@ export default class Runtime {
initializeImportMeta: (meta: JestImportMeta) => {
meta.url = pathToFileURL(modulePath).href;

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

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 (see https://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 a file:// protocol, I think? Due to the pathToFileURL on line 521

Copy link
Contributor Author

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.

@alesmenzel alesmenzel force-pushed the am/add-import-meta-dirname-and-filename branch from 4c44b56 to 7abba69 Compare January 15, 2024 11:17
@alesmenzel alesmenzel force-pushed the am/add-import-meta-dirname-and-filename branch from b37f48d to d04fb81 Compare January 15, 2024 14:53
@@ -520,6 +520,11 @@ export default class Runtime {
initializeImportMeta: (meta: JestImportMeta) => {
meta.url = pathToFileURL(modulePath).href;

// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected]
meta.filename = fileURLToPath(meta.url);
Copy link
Member

@SimenB SimenB Jan 15, 2024

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
Contributor Author

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
Contributor Author

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

SimenB commented Jan 15, 2024

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

@alesmenzel
Copy link
Contributor Author

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

Right, that will be the opposite slash probably.

jest: expect.anything(),
url: expect.any(String),
});
expect(import.meta.jest).toBe(jestObject);
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 a file:// thing, it should use forward slashes. See examples in https://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 what import.meta.filename and import.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.js
import { 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.mjs
C:\Users\G-Rath\workspace\projects-oss\jest\file.mjs
file:///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)

Copy link
Contributor Author

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

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks!

@SimenB SimenB merged commit bdb86aa into jestjs:main Jan 16, 2024
16 of 17 checks passed
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 Feb 16, 2024
@SimenB
Copy link
Member

SimenB commented Feb 20, 2024

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.

3 participants