Skip to content

Automated Testing: Restrict dirname globals in test files - #80275

Open
aduth wants to merge 1 commit into
trunkfrom
update/enforce-import-meta-dirname
Open

Automated Testing: Restrict dirname globals in test files#80275
aduth wants to merge 1 commit into
trunkfrom
update/enforce-import-meta-dirname

Conversation

@aduth

@aduth aduth commented Jul 14, 2026

Copy link
Copy Markdown
Member

What?

Updates ESLint rules to restrict the availability of __dirname and __filename globals in test files, and fixes existing issues.

This is a continuation from #79362. #79362 made import.meta values available in these files. This pull request enforces their usage.

Why?

See rationale in #79362, specifically:

We write our tests as if they're ESM, but we continue to use __dirname in tests despite the fact that __dirname doesn't exist in true ESM.

How?

Uses no-restricted-globals ESLint rule to forbid use of these globals. As explained in the inline code comment, I'd rather have done this by preventing those globals from being available in the first place rather than after-the-fact, and we could do this in ESLint through how globals are specified, but globals enforcement through no-undef is disabled through use of recommended typescript-eslint configuration (source).

Updates are straightforward replacement of __dirname ➡️ import.meta.dirname and __filename ➡️ import.meta.filename.

Testing Instructions

npm run lint:js should pass.

For bonus points, try adding __dirname or __filename to a test file and verify that npm run lint:js fails.

Use of AI Tools

Used Cursor IDE + Auto (likely Composer) model to research and implement, though mostly on the side of updating files.

@aduth
aduth requested review from a team, ajitbohra, nerrad, ntwb and ryanwelcher as code owners July 14, 2026 20:58
@aduth aduth added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Jul 14, 2026
@github-actions github-actions Bot added [Package] Theme /packages/theme [Package] UI /packages/ui labels Jul 14, 2026
@github-actions

Copy link
Copy Markdown

Size Change: 0 B

Total Size: 7.72 MB

compressed-size-action

Comment thread tools/eslint/config.mjs
Comment on lines +593 to +596
files: [ '**/@(__tests__|test)/**/*.[tj]s?(x)' ],
// Jest harness under test/unit is plain CommonJS loaded by Node/Jest
// directly (not Babel-transformed), so import.meta.* is unavailable.
ignores: [ 'test/unit/**' ],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this override also matches test/e2e/** and test/performance/**, which do not use the babel-plugin-transform-import-meta transformer added in #79362

Playwright transpiles these files as CommonJS and every end-to-end shard now stops while loading test/e2e/playwright.config.ts, and the performance job stops on its config.

Could we exclude the Playwright trees from this rule and restore their __dirname / __filename usages until that runtime has an equivalent supported transform?

Alternatively, the Playwright loader would need to be made genuinely ESM-compatible before enforcing import.meta there. Fixing only the two config files would not be enough because the converted specs and setup module use the same CommonJS/import.meta mix.

Comment thread tools/eslint/config.mjs
// globals configuration, but `no-undef` is disabled for TypeScript files,
// and TypeScript doesn't provide a way to disable the CommonJS globals.
{
files: [ '**/@(__tests__|test)/**/*.[tj]s?(x)' ],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This pattern may not cover all tests.

For example, tools/validation/validate-package-contents.test.js is collected and Babel-transformed by the root Jest config, still uses __dirname, and does not match this glob.

Maybe we should reuse the same glob used normally by Jest (*.test.* / *.spec.*) in addition to the directory patterns, while keeping the runtime-specific ignores? That would make the enforcement match the PR's stated scope.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, this one was inspired by an existing one in use already in developmentFiles. We have quite a few different patterns in this file, and it sounds like coverage is mixed. We should probably consolidate and find a pattern (like the one you suggest) that aligns well to what the tests actually execute.

'**/@(__mocks__|__tests__|test)/**/*.[tj]s?(x)',

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: aduth <aduth@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Theme /packages/theme [Package] UI /packages/ui [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants