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

docs: global type recommendation #6676

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Vitest uses Vite SSR primitives to run tests which has [certain pitfalls](https:
- **Default:** `false`
- **CLI:** `--globals`, `--globals=false`

By default, `vitest` does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the `--globals` option to CLI or add `globals: true` in the config.
By default, Vitest does not provide global APIs for testing to encourage explicitness and reduce potential conflicts. If you prefer using global APIs like Jest, you can enable them by passing the `--globals` option in the CLI or setting `globals: true` in your config file.

```ts
// vitest.config.ts
Expand All @@ -366,18 +366,45 @@ export default defineConfig({
})
```

To get TypeScript working with the global APIs, add `vitest/globals` to the `types` field in your `tsconfig.json`
#### TypeScript integration

```json
// tsconfig.json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```
To register global API types, follow one of the following methods:

1. Use a `@types/vitest.d.ts` file

Create a `@types/vitest.d.ts` file in your project root that imports `vitest/globals`:

```ts
import 'vitest/globals'
```
Copy link
Author

@privatenumber privatenumber Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experiments, this is the best method because it's clear, doesn't require any hack like import type {}, doesn't pollute source code, and works even if types/typeRoots are overridden in tsconfig.json.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also mention it here in the docs?


2. Type-import the global types from a source file

Add a type-import from `vitest/globals` in any `.ts` file (e.g., your [setup file](#setupfiles)). This registers the global APIs types automatically. Ensure the file is included in your `tsconfig.json` so TypeScript knows it's apart of the same project.

```ts
// test-setup.ts
import type {} from 'vitest/globals'
Copy link
Author

@privatenumber privatenumber Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to use an explicit type import here so it can be stripped by the compiler.

If there's no type annotation, it will be left to be resolved at run-time, and it can fail if there's no default condition:
https://github.com/vitest-dev/vitest/blob/main/packages/vitest/package.json#L37-L39

If there can be a default condition pointing to an empty file, we can simplify the recommendation to import 'vitest/globals' but I think it's better to be explicit.

```

3. Configure it in [`tsconfig.json#compilerOptions.types`](https://www.typescriptlang.org/tsconfig/#types)

Alternatively, add `vitest/globals` to the `types` field in `tsconfig.json`.

Warning: This disables TypeScript's automatic type detection, which could break your setup if your project relies on it. If you encounter types missing from other packages, you'll now need to manually register them here as well.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not even include this method as many beginners have been following this without understanding how disruptive it is and makes TS harder for them to work with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vitepress has a ::: danger block we can use here


```json5
// tsconfig.json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```

#### Auto-import

If you are already using [`unplugin-auto-import`](https://github.com/antfu/unplugin-auto-import) in your project, you can also use it directly for auto importing those APIs.
If you are already using [`unplugin-auto-import`](https://github.com/antfu/unplugin-auto-import) in your project, you can simplify this process by automatically importing the Vitest APIs without having to manually manage global type imports. This also generates TypeScript declarations for you.

```ts
// vitest.config.ts
Expand Down