vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.08k stars 1.17k forks source link

type check not work #6478

Closed rikisamurai closed 2 months ago

rikisamurai commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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.

Edit: Duplicate, slow network on train 🫠

AriPerkkio commented 2 months ago

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 commented 2 months ago

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

AriPerkkio commented 2 months ago

Thanks @mrazauskas, let's track this on https://github.com/vitest-dev/vitest/issues/3752.