Skip to content

new URL(..., import.meta.url) is not resolved in test files since 0.34 #3935

@kherock

Description

@kherock

Describe the bug

Prior to Vitest 0.34, when using the 'jsdom' environment, I was able to write components that imported assets using the new URL('@my-styleguide/brand/logo.png', import.meta.url) syntax, and the resulting paths would run through a resolver, and ultimately render as URLs resembling https://localhost:3000/@fs/.../node_modules/@my-styleguide/brand/logo.png. Since we use snapshot testing, I used a resolver hook to normalize these URLs into consistent URLs relative to the monorepo project root.

However, since 0.34, this transformation is no longer happening and URLs all resolve with file:///. This usually only happens in 0.33 when the ssr transform mode is used.

There's another potentially related issue in Vite where dependencies optimized through esbuild don't have their new URL(..., import.meta.url) expressions transformed:

vitejs/vite#8427

Reproduction

https://github.com/kherock/vitest-new-url-bug

To reproduce the issue in the repo above, clone it, and upgrade it to vitest 0.34 by running npm i -D vitest@0.34.

System Info

System:
    OS: macOS 13.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 48.28 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.20.1 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.4 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 115.0.5790.170
    Firefox: 102.12.0
    Safari: 16.5.1
  npmPackages:
    vite: ^4.4.9 => 4.4.9 
    vitest: ^0.34.1 => 0.34.1

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