vitest: Don't respect `conditions` on Vite 6

Describe the bug

  • resolve.conditions is not respected on Vite 6 with Vitest.
  • It works on Vite 6 without Vitest.
  • It works on Vite 5.
// vitest.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  resolve: {
    conditions: ['dev'],
  },
})

Reproduction

https://stackblitz.com/~/github.com/sxzz/vitest-conditions-repro

  • Run pnpm run test, and you will see the test has failed.
  • Downgrade to vite@5, remove node_modules and pnpm-lock.yaml, and it works.

System Info

N/A

Used Package Manager

pnpm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 19 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Does this also means vitest 3 (beside < vitest 2.17) will be the first version which supports vite 6 or will you also release a vitest 2.x version which supports vite 6?

There will be no more Vitest 2 versions. Vitest 3 is the first version that supports Vite 6 (excluding 2.1.6)

https://github.com/vitest-dev/vitest/releases/tag/v2.1.7 reverted support for Vite 6 for now. We will discuss with the team on how to introduce it without breaking changes.

We will be releasing Vitest 3 in January with this change instead of Vitest 2.2. Both Vite 5 and Vite 6 will be supported, but the behaviour will depend on the version installed by your package manager.

I think this needs to be considered a breaking change with vitest 2.1.5 -> 2.1.6. While I understand vitest supports 5.x and 6.x versions of vite now, in projects that don’t use vite 6.x is now automatically used over 5.x in a patch version, which can result in drastically different behaviour.

For example, with Cloudflare’s vitest-pool-workers which is used for testing Cloudflare Workers, they rely heavily on setting conditions: https://github.com/cloudflare/workers-sdk/blob/ff8f5ae03cd4a71bf7b468fcf3cd28bf064059ab/packages/vitest-pool-workers/src/config/index.ts#L110-L113

Most Cloudflare Workers don’t use vite to build though, so don’t have vite pinned to 5.x, resulting in breaking behaviour when upgrading from vitest 2.1.5 to 2.1.6, which just happened to me and led me down this rabbit hole.

I don’t think this is part of Vitest API, this is how Vite works. If you don’t want to use Vite 6, use Vite 5. We support both versions and just follow the configuration supported by the version of Vite that you are using.

This is expected behaviour in Vite 6: https://vite.dev/guide/migration.html#general-changes

The default values for those options are updated to the corresponding values and ssr.resolve.conditions no longer uses resolve.conditions as the default value.

Vitest uses ssr environment in node Vitest environment (previously, ssr field in the config), and client environment in jsdom or happy-dom (previously, the root config)

We can add this to documentation

Workaround: Adding environments.ssr.resolve.conditions can fix this issue.

import { defineConfig } from 'vitest/config'

export default defineConfig({
  resolve: {
    conditions: ['dev'],
  },
  environments: {
    ssr: {
      resolve: {
        conditions: ['dev'],
      },
    },
  },
})

Also experiencing this issue, but I’m not using vite, it’s the bump of vitest to v2.1.6 that is causing the issue for me, sticking to v2.1.5 for now