Skip to content

Don't respect conditions on Vite 6Β #6992

Closed

Description

@sxzz

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

Activity

  1. colinlienard commented on Nov 30, 2024

    @colinlienard

    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

  2. sxzz commented on Nov 30, 2024

    @sxzz
    ContributorAuthor

    Vitest uses Vite at its core, and Vitest v2.1.5 uses Vite 5, so this issue can be avoided on v2.1.5.

  3. sxzz commented on Nov 30, 2024

    @sxzz
    ContributorAuthor

    Investigating: maybe related to Environment API

  4. sxzz commented on Nov 30, 2024

    @sxzz
    ContributorAuthor

    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'],
          },
        },
      },
    })
  5. sheremet-va commented on Nov 30, 2024

    @sheremet-va
    Member

    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

  6. sxzz commented on Nov 30, 2024

    @sxzz
    ContributorAuthor

    IMHO, I don't think Vite 6's documentation is relevant to this issue. The breaking changes in Vite 6 only mention default values, but my reproduction uses a custom dev value instead, without default value.

    I also tried setting ssr.resolve.conditions: ['dev'], but it doesn't work.

  7. sheremet-va commented on Nov 30, 2024

    @sheremet-va
    Member

    The breaking changes in Vite 6 only mention default values, but my reproduction uses a custom dev value instead, without default value.

    Vite 6 doesn't inherit resolve.conditions in SSR. Vitest uses SSR environment for the default node environment (from the reproduction). The only bug here is that Vitest doesn't inherit ssr.resolve.conditions. It treats should treat resolve.conditions the same way Vite does.

  8. sheremet-va commented on Nov 30, 2024

    @sheremet-va
    Member

    You can override transformMode, by the way: https://vitest.dev/config/#testtransformmode (by default, it depends on the environment)

  9. sxzz commented on Nov 30, 2024

    @sxzz
    ContributorAuthor

    I see. Maybe Vitest should release a major version due to the breaking change in Vite that significantly affects Vitest.

  10. sheremet-va commented on Nov 30, 2024

    @sheremet-va
    Member

    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.

  11. kolorfilm commented on Dec 1, 2024

    @kolorfilm

    The breaking changes in Vite 6 only mention default values, but my reproduction uses a custom dev value instead, without default value.

    Vite 6 doesn't inherit resolve.conditions in SSR. Vitest uses SSR environment for the default node environment (from the reproduction). The only bug here is that Vitest doesn't inherit ssr.resolve.conditions. It treats should treat resolve.conditions the same way Vite does.

    I have the same problem. Can you maybe explain how this could look like in a workaround? Using vitest with happy dom.

    Thank you!

  12. kolorfilm commented on Dec 1, 2024

    @kolorfilm

    Somehow has the same issue.

  13. 12 remaining items

  14. WtfJoke commented on Dec 5, 2024

    @WtfJoke

    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?

  15. sheremet-va commented on Dec 5, 2024

    @sheremet-va
    Member

    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)

  16. moved this from P2 - 5 to Approved in Team Boardon Dec 5, 2024
  17. added
    p3-significant
    and removed
    p2-to-be-discussed
    on Dec 5, 2024
  18. unicornware commented on Jan 27, 2025

    @unicornware

    @sheremet-va

    conditions passed through NODE_OPTIONS are also not respected, which means internal imports are no longer resolved properly.

    ✘ [ERROR] Could not resolve "#colors"
    
        __tests__/reporters/verbose.mts:7:19:
          7 β”‚ import colors from '#colors'
            β•΅                    ~~~~~~~~~
    
      The module "./dist/colors.mjs" was not found on the file system:
    
        package.json:54:17:
          54 β”‚       "default": "./dist/colors.mjs"
             β•΅                  ~~~~~~~~~~~~~~~~~~~
    
      You can mark the path "#colors" as external to exclude it from the bundle, which will remove this
      error and leave the unresolved path in the bundle.
    
    failed to load config from /Users/lex/Projects/FLDV/FLDV-P061/vitest.config.mts

    note: the suggestion provided does not fix the error.

    pinning vite to version 5.4.14 seems to be the only workaround. unfortunately, this isn't ideal. is this expected behavior?

  19. hi-ogawa commented on Jan 28, 2025

    @hi-ogawa
    Collaborator

    Closing since this is an intended breaking change on Vitest 3 and OP's reproduction passes now with ssr.resolve.conditions: ['dev'] https://stackblitz.com/edit/github-bf27lxf7?file=vitest.config.ts

    If you encounter an issue, please create a new issue with the reproduction. Thanks!

  20. locked and limited conversation to collaborators on Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changep3-significant

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions