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

fix(jest): support ESM setup files #12

Merged
merged 6 commits into from
Oct 13, 2024

Conversation

sparten11740
Copy link
Contributor

@sparten11740 sparten11740 commented Oct 3, 2024

Add support for importing ESM files supplied in jest's setupFiles or setupFilesAfterEnv

@sparten11740 sparten11740 self-assigned this Oct 3, 2024
@@ -0,0 +1,3 @@
const path = require('path')

global.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works
Copy link
Collaborator

@ChALkeR ChALkeR Oct 5, 2024

Choose a reason for hiding this comment

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

globalThis is the canonical name for the global object
global might be not defined in general (e.g. if we support some other env other than Node.js-like)

% jsc 
>>> globalThis
[object global]
>>> global
Exception: ReferenceError: Can't find variable: global

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced with globalThis

@@ -0,0 +1,3 @@
import path from 'path'

global.SETUP_JS_MODULE = path.basename(import.meta.url)
Copy link
Collaborator

Choose a reason for hiding this comment

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

import.meta.url is an url, calling basename on it might be not ideal
does just import.meta.filename work here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah, this is actually testing that import path works, makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, these 3 setup files are just for testing.

Copy link
Collaborator

@ChALkeR ChALkeR left a comment

Choose a reason for hiding this comment

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

Let's land this and if anything needs to be fixed will change it later

@ChALkeR ChALkeR merged commit 2c923a0 into main Oct 13, 2024
9 checks passed
@sparten11740 sparten11740 mentioned this pull request Oct 16, 2024
@ChALkeR ChALkeR deleted the sparten11740/fix/import-setup-files branch October 24, 2024 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants