vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.76k stars 1.15k forks source link

Dependencies are not inlined when using ssr.noExternal with globs #5911

Open brandonroberts opened 3 months ago

brandonroberts commented 3 months ago

Describe the bug

The docs state that anything in the ssr.noExternal array in the Vite config is automatically added to the test.server.deps.inline array.

Globs do not seem to be supported.

Repo: https://github.com/eduardoRoth/analog-ionic-capacitor-template

Reproduction

  1. Clone the repo - https://github.com/eduardoRoth/analog-ionic-capacitor-template
  2. Install the dependencies - npm i
  3. Go into the vite.config.ts
  4. Remove @ionic/angular from the test.server.deps.inline array. @ionic/** is already in the ssr.noExternal array.
  5. Run npx ng test
  6. Note the error, stop the tests
  7. Go into the ssr.noExternal array
  8. Add @ionic/angular
  9. Run npx ng test
  10. Note the tests pass

System Info

System:
    OS: macOS 12.6.8
    CPU: (10) arm64 Apple M1 Pro
    Memory: 231.17 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
    pnpm: 9.3.0 - ~/Library/pnpm/pnpm
    bun: 1.0.35 - ~/.bun/bin/bun
  Browsers:
    Chrome: 126.0.6478.61
    Safari: 15.6.1
  npmPackages:
    vite: ~5.2.11 => 5.2.13 
    vitest: ^1.3.1 => 1.6.0

Used Package Manager

npm

Validations

hi-ogawa commented 3 months ago

Indeed, glob doesn't seem to be supported for server.deps.inline.

Vite uses createFilter utility https://github.com/vitejs/vite/blob/ec16a5efc04d8ab50301d184c20e7bd0c8d8f6a2/packages/vite/src/node/ssr/ssrExternal.ts#L38-L41 to support glob, but Vitest (vite-node) has a different logic to match patterns https://github.com/vitest-dev/vitest/blob/f969fb0f9f0247a7daa2afee8f70de25ea5e123f/packages/vite-node/src/externalize.ts#L157-L162

I created a repro with simpler dependencies https://github.com/hi-ogawa/reproductions/tree/main/vitest-5911-deps-inline-glob

brandonroberts commented 3 months ago

Thanks @hi-ogawa. I can work on a PR to support this behavior if that's whats recommended

sheremet-va commented 3 months ago

vite-node processes ssr.noExternal on resolved file paths, Vite processes it on bare imports, so we cannot apply the same filter.

brandonroberts commented 3 months ago

vite-node processes ssr.noExternal on resolved file paths, Vite processes it on bare imports, so we cannot apply the same filter.

Ok. What would be the recommended approach with globs?

sheremet-va commented 3 months ago

Ok. What would be the recommended approach with globs?

If I knew what to do with this, we would've already implemented it 😞

I don't know how to solve these, they were always resolved differently in Vite and Vitest. Vite can also resolve monorepo packages while Vitest expects that the package is in moduleDirectories and the name and the folder are the same.

If it's fine to keep this difference, we can just apply the glob with the same rule - svelte/* will be equal to node_modules/svelte/.+

brandonroberts commented 3 months ago

Thanks @sheremet-va. I think it's fine to keep this difference, and note that globs are not expected to be inlined also