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

Failed to load url error when using sveltekit and jsdom environment (0.34.1) #3866

Closed
6 tasks done
EugeneDraitsev opened this issue Aug 2, 2023 · 3 comments
Closed
6 tasks done

Comments

@EugeneDraitsev
Copy link

Describe the bug

I have "Failed to load url" error when using sveltekit and jsdom environment for setupFiles file. It works fine with vitest 0.33, this error appear only in vitest 0.34.1

Reproduction

  1. Create empty sveltekit project with vitest npm create svelte@latest my-app or clone my repo with all dependencies and files: https://github.com/EugeneDraitsev/vitest-setup-files-bug
  2. Add jsdom to dependencies, add empty setupTest.ts and use following vite.config.ts
export default defineConfig({
	plugins: [sveltekit()],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}'],
		environment: 'jsdom',
		setupFiles: './setupTest.ts'
	}
});
  1. Run yarn test and see "Failed to load url" error

If you comment environment: 'jsdom', or downgrade vitest to 0.33.0, then it will work fine

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700KF
    Memory: 13.58 GB / 31.82 GB
  Binaries:
    Node: 18.15.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    Yarn: 1.22.19 - ~\scoop\apps\yarn\current\bin\yarn.CMD
    npm: 9.5.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1992.0), Chromium (115.0.1901.188)
    Internet Explorer: 11.0.22621.1

Used Package Manager

yarn

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Aug 2, 2023

Previously Vitest was running in SSR mode even if environment was JSDOM.

Vite doesn't allow accessing files outside of the root folder by default. Since 0.34.0, Vitest uses web mode to load all files inside a test suite, and because of this Vite fallbacks to fs.allow rules. You need to allow files outside of root to be served manually:

export default defineConfig({
	plugins: [sveltekit()],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}'],
		environment: 'jsdom',
		setupFiles: './setupTest.ts'
	},
	server: {
		fs: {
			allow: ['./setupTest.ts']
		}
	}
});

For security concerns, it's probably better to check for process.env.VITEST when allowing those files to be server:

   {
		fs: {
			allow: process.env.VITEST ? ['./setupTest.ts'] : undefined
		}
	}

@sheremet-va
Copy link
Member

We should probably add those files on the Vitest side, actually.

@sheremet-va sheremet-va reopened this Aug 2, 2023
@sheremet-va sheremet-va added bug and removed wontfix labels Aug 2, 2023
@EugeneDraitsev
Copy link
Author

Thank you for explanation! It works fine with server configuration

@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants