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

type check not work #6478

Closed
6 tasks done
rikisamurai opened this issue Sep 12, 2024 · 6 comments
Closed
6 tasks done

type check not work #6478

rikisamurai opened this issue Sep 12, 2024 · 6 comments

Comments

@rikisamurai
Copy link

Describe the bug

I followed the official examples to use Vitest and Vite for type checking, but even when there are type errors, no errors are reported, and the test cases still pass.

import { expect, expectTypeOf, test } from 'vitest'

test('type', () => {
    expectTypeOf(1).toEqualTypeOf('2')
    expectTypeOf({ a: 11 }).toEqualTypeOf<{ a: string }>();
    expect(1).toBe(2) // not executed
})

there are type errors, but still pass.

Reproduction

stackBlitz: https://stackblitz.com/~/github.com/rikisamurai/vitest-issue

or

 npm create vite@latest my-app -- --template react-ts

and follow https://github.com/vitest-dev/vitest/blob/main/examples/typecheck/test/type.test-d.ts

System Info

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 170.70 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 9.10.0 - ~/Library/pnpm/pnpm
    bun: 1.1.26 - ~/.bun/bin/bun
  Browsers:
    Chrome: 128.0.6613.121
    Safari: 17.2.1
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1 
    @vitest/ui: ^2.0.5 => 2.0.5 
    vite: ^5.4.1 => 5.4.2 
    vitest: ^2.0.5 => 2.0.5

Used Package Manager

pnpm

Validations

@AriPerkkio
Copy link
Member

AriPerkkio commented Sep 12, 2024

Your tsconfig.json doesn't include any files.

{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}

By default Vitest uses the tsconfig.json that it can find: https://vitest.dev/config/#typecheck-tsconfig

So to make your repro work, let's define one of those custom tsconfig.json files in Vitest config:

export default defineConfig({
  plugins: [react()],
  test: {
    typecheck: {
      enabled: true,
+     tsconfig: 'tsconfig.app.json'
    },
  },
})

image

@rikisamurai
Copy link
Author

Your tsconfig.json doesn't include any files.

{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}

By default Vitest uses the tsconfig.json that it can find: https://vitest.dev/config/#typecheck-tsconfig

So to make your repro work, let's define one of those custom tsconfig.json files in Vitest config:

export default defineConfig({
  plugins: [react()],
  test: {
    typecheck: {
      enabled: true,
+     tsconfig: 'tsconfig.app.json'
    },
  },
})

image

Thank you very much for your help❤️!
Is it expected that Vitest won't refer to references in this situation? Many beginners, like me, might encounter this issue after creating a project using the Vite template and then adding Vitest.

@AriPerkkio

This comment was marked as duplicate.

@AriPerkkio
Copy link
Member

Oh right, there's references. Vitest uses tsc --noEmit --pretty false -p tsconfig.json to run the typechecks. Looks like references do not emit any type errors when --build flag is not used.

@mrazauskas
Copy link

See #3752. The references related behaviour was discussed there.

@AriPerkkio
Copy link
Member

Thanks @mrazauskas, let's track this on #3752.

@AriPerkkio AriPerkkio closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 29, 2024
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

3 participants