Skip to content

TypeScript definitions are lost when running Vitest via @nuxt/test-utils #801

@nogic1008

Description

@nogic1008

Describe the bug

When running tests with Vitest (using @nuxt/test-utils), the database schema type definitions in node_modules/@nuxthub/db/schema.d.mts are replaced with a stub (export * from './schema.mjs') instead of preserving the full type definitions.

This causes TypeScript errors in the editor after test execution, breaking the development experience.

Steps to reproduce

  1. Set up a Nuxt project with @nuxthub/[email protected] and @nuxt/test-utils
  2. Create database schema in server/db/schema.ts
  3. Run pnpm dev - schema types are correctly generated
  4. Run pnpm test - tests pass but schema types are destroyed
  5. Check node_modules/@nuxthub/db/schema.d.mts - it becomes 28 bytes
  6. Editor shows TypeScript errors for @nuxthub/db/schema imports

Expected behavior

After running tests, node_modules/@nuxthub/db/schema.d.mts should maintain the full type definitions, not be replaced with a stub.

Environment

  • @nuxthub/core: 0.10.5
  • @nuxt/test-utils: 3.23.0
  • vitest: 4.0.17
  • nuxt: 4.3.0

Root Cause Analysis

During Vitest execution with @nuxt/test-utils, the buildDir changes from .nuxt to node_modules/.cache/nuxt/.nuxt.

The issue maybe occurs in generateDatabaseSchema():

  1. buildDatabaseSchema() generates schema files to <buildDir>/hub/db/ (=.nuxt/hub/db/)
  2. @nuxt/test-utils changes buildDir to node_modules/.cache/nuxt/.nuxt during tests
  3. The code tries to read generated schema from <buildDir>/hub/db/schema.d.mts (=node_modules/.cache/nuxt/.nuxt/hub/db/schema.d.mts)
  4. Since the file doesn't exist at that location, it falls into the catch block
  5. The catch block writes a stub: export * from './schema.mjs'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions