withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.93k stars 2.42k forks source link

[AstroDB] getViteConfig throws in Vitest - Cannot read properties of undefined (reading 'ASTRO_DATABASE_FILE') #11414

Open haivuw opened 3 months ago

haivuw commented 3 months ago

Astro Info

Astro                    v4.11.5
Node                     v20.10.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             astro:db
                         @astrojs/db/file-url

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

There's an error if I run vitest with astro's getViteConfig, and the default database .astro/content.db is not seeded:

➜  astrodb-vitest-repro git:(main) px vitest run
20:46:26 [types] Added src/env.d.ts type declarations.

 RUN  v1.6.0 /home/hl/ws/test/astrodb-vitest-repro

20:46:26 [astro:db] New local database created.
20:46:26 [ERROR] [astro:db] Cannot read properties of undefined (reading 'ASTRO_DATABASE_FILE')
 ✓ src/test.test.ts (1)
   ✓ test

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  20:46:26
   Duration  584ms (transform 8ms, setup 0ms, collect 8ms, tests 1ms, environment 0ms, prepare 43ms)

Moreover, if I import the db in the test file (import { db } from "astro:db") and set vitest's poolOptions.threads.singleThread to true, the test will fail half of the time.

What's the expected result?

No error should be showing when running vitest. Test database should be seeded.

Link to Minimal Reproducible Example

https://github.com/haivuw/astrodb-vitest-repro

Participation

haivuw commented 3 months ago

This is what it shows in my private project:

 FAIL  src/auth/verify-email.test.ts [ src/auth/verify-email.test.ts ]
TypeError: Cannot read properties of undefined (reading 'ASTRO_DATABASE_FILE')
 ❯ eval astro:db:seed:7:87
 ❯ instantiateModule node_modules/.pnpm/vite@5.3.3_@types+node@20.14.9/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:53451:5

If I run ASTRO_DATABASE_FILE=test.db pnpm exec vitest run, test.db is created as expected, but it won't be seeded, and I still see the error.