Skip to content

Fix Vite native config loader warning in Vitest runs - #1604

Merged
cristianrgreco merged 1 commit into
mainfrom
vitest-config-import-meta-dirname
Oct 14, 2026
Merged

cristianrgreco merged 1 commit into
mainfrom
vitest-config-import-meta-dirname

Conversation

@cristianrgreco

Copy link
Copy Markdown
Collaborator

Summary

Every Vitest run printed this before the tests, locally and in each CI test job:

(!) Your Vite config uses features that are unsupported by `configLoader: 'native'`, which is planned to become the default in a future major version of Vite:
  - `__dirname` (vitest.config.ts:26:36). Use `import.meta.dirname` instead
Set `VITE_CONFIG_NATIVE_IGNORE_WARNING=true` to suppress this warning.

The root package.json has "type": "module", so vitest.config.ts is an ES module and __dirname only works because Vite's default bundle loader injects it. With the native loader the config does not load at all.

The testcontainers alias now resolves from import.meta.dirname, which Node provides in ES modules from 20.11 (the repo requires >= 22.22). Nothing else in the config is rejected by the native loader: it loads and runs tests with --configLoader native.

Verification

Node v24.18.0, Vitest 5.0.3. packages/testcontainers/build did not exist during the test runs, so a module test can only import testcontainers through the alias to source.

Command Before After
npx vitest run packages/testcontainers/src/utils/port.test.ts warning printed, 10 passed no warning, 10 passed
npx vitest run --configLoader native packages/testcontainers/src/utils/port.test.ts exit 1, ReferenceError: __dirname is not defined in ES module scope at vitest.config.ts:26:36 exit 0, 10 passed
npx vitest run packages/modules/gcloud/src/emulator-flags-manager.test.ts no warning, 7 passed
npx vitest run packages/modules/gcloud/src/pubsub-emulator-container.test.ts (imports testcontainers through the alias) no warning, 1 passed
npx vitest run --configLoader native packages/modules/gcloud/src/pubsub-emulator-container.test.ts 1 passed

Before, native loader:

failed to load config from .../vitest.config.ts

⎯⎯⎯⎯⎯⎯⎯ Startup Error ⎯⎯⎯⎯⎯⎯⎯⎯
ReferenceError: __dirname is not defined in ES module scope
    at .../vitest.config.ts:26:36

After, default loader:

 RUN  v5.0.3

 Test Files  1 passed (1)
      Tests  10 passed (10)
  • npm run format, npm run lint, npm run knip and npm run check-compiles pass.
  • No tsconfig includes vitest.config.ts (package tsconfigs use rootDir: src), so it was type-checked on its own with the base options: npx tsc --noEmit --module nodenext --moduleResolution nodenext --target ES2023 --lib ES2023 --strict --skipLibCheck --types node vitest.config.ts exits 0.
  • vitest.config.ts was the only __dirname/__filename use outside package source. The uses under packages/**/src compile to CommonJS and are unchanged.

Not breaking

Only the root Vitest config changes. It is not part of any published package, and the alias resolves to the same path as before.

Vite warns that __dirname is unsupported by configLoader: 'native', which will become the default loader in a future major version.
@cristianrgreco cristianrgreco added maintenance Improvements that do not change functionality patch Backward compatible bug fix labels Oct 13, 2026
@cristianrgreco
cristianrgreco marked this pull request as ready for review October 13, 2026 17:22
@cristianrgreco
cristianrgreco merged commit b5a1141 into main Oct 14, 2026
191 checks passed
@cristianrgreco
cristianrgreco deleted the vitest-config-import-meta-dirname branch October 14, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Improvements that do not change functionality patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant