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

Bug: browser.testerScripts does not support external script URLs properly #6768

Closed
6 tasks done
valentinpalkovic opened this issue Oct 22, 2024 · 1 comment · Fixed by #6725
Closed
6 tasks done

Bug: browser.testerScripts does not support external script URLs properly #6768

valentinpalkovic opened this issue Oct 22, 2024 · 1 comment · Fixed by #6725

Comments

@valentinpalkovic
Copy link

valentinpalkovic commented Oct 22, 2024

Describe the bug

I am trying to follow the example provided on the Vitest homepage for browser.testerScripts:

browser: {
  testerScripts: [
    {
      src: 'https://code.jquery.com/jquery-3.3.1.min.js',
    },
  ],
},

According to the documentation, browser.testerScripts is a custom script injection feature for the browser environment. The script source and content should be processed by Vite plugins, and this feature seems ideal for injecting polyfills or external libraries required for certain browser tests.

However, when I attempt to inject an external script (in this case, jQuery), the URL is transformed to:

http://localhost:5173/@fs//https://code.jquery.com/jquery-3.3.1.min.js

This results in a failed request with a 404 error, as the transformation to the local server address is incorrect for external URLs.

Expected behaviour:
External URLs provided in browser.testerScripts should not be transformed by Vite or should be handled correctly so that the script can be loaded and injected into the browser test environment.

Reproduction

  1. Configure browser.testerScripts with an external script URL, e.g., jQuery:
browser: {
  testerScripts: [
    {
      src: 'https://code.jquery.com/jquery-3.3.1.min.js',
    },
  ],
},
  1. Run Vitest in the browser.
  2. Check the network tab for the failed script request with the transformed URL.

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 3.51 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.3 - ~/.nvm/versions/node/v18.20.3/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v18.20.3/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v18.20.3/bin/npm
    pnpm: 9.5.0 - ~/Library/pnpm/pnpm
    bun: 1.1.27 - ~/.bun/bin/bun
  Browsers:
    Chrome: 129.0.6668.101
    Edge: 130.0.2849.46
    Safari: 17.4.1
  npmPackages:
    @vitejs/plugin-vue: ^5.0.3 => 5.0.3 
    @vitest/browser: ^2.1.3 => 2.1.3 
    vite: ^5.0.12 => 5.0.12 
    vitest: ^2.1.3 => 2.1.3

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

True, it was added specifically for that. In the next patch, the option will be deprecated and instead, Vitest will use transformIndexHtml (see #6725), so the fix for the future is to inject it in the plugin.

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

Successfully merging a pull request may close this issue.

2 participants