kit icon indicating copy to clipboard operation
kit copied to clipboard

`vitest` hangs when using `adapter-cloudflare`

Open wooseopkim opened this issue 1 year ago • 5 comments

Describe the bug

Everything seems okay when using adapter-auto. However vitest hangs With adapter-cloudflare. I saw this issue and tried pool: 'forks' but it didn't fix the problem.

Reproduction

https://github.com/wooseopkim/sveltekit-vitest-repro

This repository contains source code generated with npm create svelte@latest just with the additional dependency of @sveltejs/adapter-cloudflare. Run npm run test -- --run to see the problem.

Logs

Below is the output with hanging-reporters in vite.config.js.

There are 8 handle(s) keeping the process running

# Tinypool
node:internal/async_hooks:202                                                                                
node:internal/async_hooks:505                                                                                
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/tinypool/dist/esm/index.js:37              
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/tinypool/dist/esm/index.js:58              
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/tinypool/dist/esm/index.js:952             
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/vitest/dist/vendor/cli-api.E07AF1Yq.js:8192
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/vitest/dist/vendor/cli-api.E07AF1Yq.js:8845
file:///home/<user>/Workspace/sveltekit-vitest-repro/node_modules/vitest/dist/vendor/cli-api.E07AF1Yq.js:8870

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202

# FILEHANDLE
node:internal/async_hooks:202
close timed out after 10000ms
Tests closed successfully but something prevents Vite server from exiting
You can try to identify the cause by enabling "hanging-process" reporter. See https://vitest.dev/config/#reporters

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 7600 6-Core Processor
    Memory: 47.86 GB / 54.93 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.14.0 - ~/.volta/tools/image/node/20.14.0/bin/node
    Yarn: 4.2.2 - ~/.volta/tools/image/yarn/4.2.2/bin/yarn
    npm: 10.7.0 - ~/.volta/tools/image/node/20.14.0/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.2.1 
    @sveltejs/adapter-cloudflare: ^4.4.0 => 4.4.0 
    @sveltejs/kit: ^2.0.0 => 2.5.10 
    @sveltejs/package: ^2.0.0 => 2.3.1 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.1 
    svelte: ^4.2.7 => 4.2.17 
    vite: ^5.0.11 => 5.2.12 

Severity

annoyance

Additional Information

No response

wooseopkim avatar Jun 06 '24 05:06 wooseopkim

Adding statement await proxy.dispose(); here seems to fix this problem, but leads to another (and more serious) problem - dev server loses connection to local Miniflare.

wooseopkim avatar Jun 06 '24 06:06 wooseopkim

I've found a better, potential fix: moving this line into the vite.middlewares.use block. I have no idea what the side effects are as of now, though.

wooseopkim avatar Jun 06 '24 06:06 wooseopkim

I know this is a ridiculous fix but

"test:unit": "sed -i '1s/.*/import adapter from \"@sveltejs\\/adapter-auto\";/' svelte.config.js && vitest run && sed -i '1s/.*/import adapter from \"@sveltejs\\/adapter-cloudflare\";/' svelte.config.js"

Oreoezi avatar Jul 09 '24 16:07 Oreoezi

I have also hit the issue. It renders unit testing unusable with no obvious workaround except for downgrading to 4.1.0 which is getting a fair way behind the latest.

bdmackie avatar Oct 07 '24 11:10 bdmackie

These are the logs when it hangs (after upgrading everything to the latest).

❯ npm run test -- --run

> [email protected] test
> vitest --run


 RUN  v2.1.3 /home/chewteeming/github/sveltekit-vitest-repro

 ✓ src/index.test.js (1)
   ✓ sum test (1)
     ✓ adds 1 + 2 to equal 3

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  13:44:29
   Duration  239ms (transform 11ms, setup 0ms, collect 9ms, tests 2ms, environment 0ms, prepare 61ms)

close timed out after 10000ms
Tests closed successfully but something prevents Vite server from exiting
You can try to identify the cause by enabling "hanging-process" reporter. See https://vitest.dev/config/#reporter

teemingc avatar Oct 18 '24 05:10 teemingc

My current temporary solution is to switch to adapter-auto when running vitest based on process.env.VITEST:

// svelte.config.js

import { default as auto} from '@sveltejs/adapter-auto';
import { default as cf } from '@sveltejs/adapter-cloudflare';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
  preprocess: vitePreprocess(),

  kit: {
    adapter: process.env.VITEST ? auto() : cf()
  }
};

export default config;

knicholson32 avatar Oct 30 '24 03:10 knicholson32

Thanks @knicholson32. That works, however it does require a different approach to passing env vars. e.g. using .env.local with the .dev.vars subsets you need in tests, loading env from vite.config.ts, prepending with VITE_ and checking for VITE_ prefix in the code.

bdmackie avatar Oct 30 '24 06:10 bdmackie