Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new URL throw error when environment is happy-dom in 0.34.2 #3988

Open
6 tasks done
himself65 opened this issue Aug 20, 2023 · 15 comments
Open
6 tasks done

new URL throw error when environment is happy-dom in 0.34.2 #3988

himself65 opened this issue Aug 20, 2023 · 15 comments

Comments

@himself65
Copy link

Describe the bug

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/basic.test.ts [ test/basic.test.ts ]
TypeError: The URL must be of scheme file
 ❯ TypeError.get ../../../blitz.ba0db7b1.js:67:7048

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'ERR_INVALID_URL_SCHEME' }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests   (1)
   Start at  13:20:13
   Duration  1.10s (transform 33ms, setup 24ms, collect 10ms, tests 3ms, environment 191ms, prepare 82ms)


 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit
 
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: [birpc] timeout on calling "onTaskUpdate"
 ❯ Timeout.eval node_modules/vitest/dist/vendor-index.b271ebe4.js:52:22
 ❯ listOnTimeout node:internal/timers:62:4448
 ❯ processTimers node:internal/timers:62:5285
 ❯ <anonymous> ../../../blitz.ba0db7b1.js:334:285414
 ❯ <anonymous> ../../../blitz.ba0db7b1.js:334:285713

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-3da18m?file=setup.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.10 - /usr/local/bin/pnpm
  npmPackages:
    vite: latest => 4.4.9

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented Aug 20, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@himself65 himself65 changed the title new URL throw error when environment is happy-dom in 0.34.2 new URL throw error when environment is happy-dom in 0.34.2 Aug 20, 2023
@silverwind
Copy link
Contributor

silverwind commented Aug 21, 2023

Happens with environment: jsdom as well. In v0.34.1 setupFiles had executed in Node, and import.meta.url resolved to a file:// url, but it seems since v0.34.2, it now executes in environment and resolves to http://localhost:3000, breaking it.

Specifically, I'm loading JSON fixtures from the file system in my setupFiles, so I think it definitely should execute in Node.

@sheremet-va
Copy link
Member

sheremet-va commented Aug 21, 2023

Happens with environment: jsdom as well. In v0.34.1 setupFiles had executed in Node, and import.meta.url resolved to a file:// url, but it seems since v0.34.2, it now executes in environment and resolves to http://localhost:3000, breaking it.

Specifically, I'm loading JSON fixtures from the file system in my setupFiles, so I think it definitely should execute in Node.

This is expected behavior. Web transform mode does the same transformations that Vite does for browsers, and it transforms new URL('./path', import.meta.url) to an assets URL. Vite replaces new URL with this:

new URL('/package.json', self.location)

To bypass this, I guess you can store import.meta.url separately because it's a regexp match.

You can find the change here:

@silverwind
Copy link
Contributor

Would import './package.json' work or does that fall in the same category?

@silverwind
Copy link
Contributor

I switched to import './package.json' and it appears to work fine in v0.34.2. I think it's actually vite's json loader that loads this here because if it would be node's it would have required a import assertion.

@sheremet-va
Copy link
Member

I switched to import './package.json' and it appears to work fine in v0.34.2. I think it's actually vite's json loader that loads this here because if it would be node's it would have required a import assertion.

Importing .json files works in Vitest because vite-node doesn't have Node.js limitations. This will change for SSR environments in the future, but for now, it's working, yes.

@silverwind
Copy link
Contributor

silverwind commented Aug 21, 2023

I that ever breaks, I will add vite plugin to import json 😆.

@belgattitude
Copy link

belgattitude commented Aug 21, 2023

Got hit as well. Some conversion tests using csv fixtures (native import is limited in that case).

Based on my usage I've just forced // @vitest-environment node on top of the test file. Hope it helps

See https://vitest.dev/guide/environment.html#environments-for-specific-files

@belgattitude
Copy link

@sheremet-va could you give an example about

To bypass this, I guess you can store import.meta.url separately because it's a regexp match.

In my case I have a function :

const loadFixtureCsv = (file: string): ArrayBuffer => {
  return readFileSync(new URL(`./__fixtures__/${file}`, import.meta.url), {
    encoding: null,
  });
};

But I don't see how I could store import.meta.url ?

Thanks in advance

@sheremet-va
Copy link
Member

sheremet-va commented Aug 21, 2023

In my case I have a function :

I think just storing URL in a separate variable makes Vite bypass the transformation:

const baseUrl = import.meta.url
const filePath = new URL(`./__fixtures__/${file}`, baseUrl)

@belgattitude
Copy link

Indeed it works as well. Thanks for the tip.

@silverwind
Copy link
Contributor

silverwind commented Aug 21, 2023

I think using any kind of node API like readFileSync in vitest files is not ideal when environment is not node and bound to break in the future. CSV could be loaded using a plugin like vite-string-plugin. Could then likely use import.meta.glob to load multiple files with a wildcard too.

@sheremet-va
Copy link
Member

sheremet-va commented Aug 21, 2023

like vite-string-plugin

Can't you just use ?raw query?

import md from './index.md?raw'
import.meta.glob('./*.md', { as: 'raw', eager: true })

@silverwind
Copy link
Contributor

Can't you just use ?raw query?

Likely, but then eslint-plugin-import would start failing to resolve and would need extra config to work. Generally I try to avoid query parameters on imports.

@stagas
Copy link

stagas commented Feb 8, 2024

I'm encountering the same issue, the problem is I don't control the code that contains the new URL(x, import.meta.url). It's autogenerated by another tool.
Is there any other workaround maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants