Skip to content

Vitest doesn't handle package.json exports conditions like browser #2603

@segevfiner

Description

@segevfiner

Describe the bug

We have a package that has a browser ESM & Node CJS & ESM builds. So exports looks something like this:

  "exports": {
    "types": "./dist/node.d.ts",
    "browser": "./dist/browser.mjs",
    "import": "./dist/node.mjs",
    "require": "./dist/node.js"
  },

Vitest selects the Node ESM build even with environment: "jsdom". Annoyingly, this can be a good thing for some packages, and bad for others in tests.

See https://jestjs.io/docs/29.0/configuration#testenvironmentoptions-object for the config and special comment syntax for this issue supported by Jest.

This might be difficult to support without loader hooks though Node does have a --conditions flag.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-4jwegg?file=test/basic.test.ts

System Info

System:
    OS: macOS 12.6.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 123.48 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - ~/.nvm/versions/node/v16.19.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.19.0/bin/yarn
    npm: 9.2.0 - ~/.nvm/versions/node/v16.19.0/bin/npm
  Browsers:
    Chrome: 108.0.5359.124
    Safari: 16.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions