vitest-dev / vitest-dev/vitest

Astro typecheck prevents astro components from being loaded

Open
#9,428 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

Description

Describe the bug

I've created an astro project with basic vite configuration:

/// <reference types="vitest" />
import { getViteConfig } from 'astro/config';
import tsconfigPaths from 'vite-tsconfig-paths';

export default getViteConfig({
  plugins: [tsconfigPaths()],
  // @ts-expect-error - Vitest config is merged
  test: {
    include: ['tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
    typecheck: {
      enabled: true,
      checker: 'tsc',
      include: ['tests/**/*.{test,spec}-d.{ts,mts,cts}'],
    },
  },
});

I've created a polymorphic component and testing for the tests:

import type { ComponentProps, HTMLTag } from 'astro/types';
import { expectTypeOf, test } from 'vitest';
import type Polymorphic from '@/components/html/_polymorphic.astro';

test('Polymorphic types', () => {
  type PolymorphicProps<Tag extends HTMLTag> = ComponentProps<
    typeof Polymorphic<Tag>
  >;
  type ButtonProps = PolymorphicProps<'button'>;

  expectTypeOf<ButtonProps['as']>().toEqualTypeOf<'button' | undefined>();

  // Check specific attributes
  type AnchorProps = PolymorphicProps<'a'>;
  expectTypeOf<AnchorProps['as']>().toEqualTypeOf<'a' | undefined>();

  // href should exist on anchor
  expectTypeOf<AnchorProps['href']>().not.toBeNever();

  // Test invalid usage
  // @ts-expect-error - 'href' is not valid on button (if strict)
  const _: ButtonProps = { as: 'button', href: 'bad' };
});

I'm receiving:

 ❯  TS  tests/html/_polymorphic.test-d.ts (1 test | 1 failed)
   × Polymorphic types

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
 FAIL  tests/html/_polymorphic.test-d.ts > Polymorphic types
TypeCheckError: Cannot find module '@/components/html/_polymorphic.astro' or its corresponding type declarations.
 ❯ tests/html/_polymorphic.test-d.ts:3:30
      1| import type { ComponentProps, HTMLTag } from 'astro/types';
      2| import { expectTypeOf, test } from 'vitest';
      3| import type Polymorphic from '@/components/html/_polymorphic.astro';
       |                              ^
      4| 
      5| test('Polymorphic types', () => {

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Reproduction

Generally doesn't matter any import of an astro component inside vite causes this. Whilst astro check with the plugin works. Maybe astro needs a plugin but the issue is the difference that was being created.

System Info
System:
    OS: Linux 6.18 Arch Linux
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 53.10 GB / 62.50 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 24.11.0 - /home/sagi/.local/share/mise/installs/node/24.11.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 11.6.1 - /home/sagi/.local/share/mise/installs/node/24.11.0/bin/npm
    pnpm: 10.27.0 - /home/sagi/.local/share/pnpm/pnpm
    bun: 1.3.1 - /home/sagi/.local/share/mise/installs/bun/1.3.1/bin/bun
  Browsers:
    Chromium: 143.0.7499.109
    Firefox: 146.0.1
    Firefox Developer Edition: 146.0.1
  npmPackages:
    vitest: ^4.0.16 => 4.0.16
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown getViteConfig setup and the failing tests/html/_polymorphic.test-d.ts import of the Astro component. Compare Vitest's typecheck behavior with the Astro check/plugin path that reportedly works. Done means Astro component imports resolve during Vitest typecheck without the reported module error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.