From 8e479cc43aed01f023cc04476ac8bfd706bf9cf1 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 10 Oct 2024 13:41:43 +0900 Subject: [PATCH] wip --- docs/config/index.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 9e2d220a9e811..7e2cf139793f9 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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 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. +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 @@ -368,18 +368,26 @@ export default defineConfig({ #### TypeScript integration -To ensure TypeScript recognizes the global APIs, you have two options: +To ensure TypeScript recognizes the global APIs, you can choose one of the following options: -1. Import the global types +1. Use a `@types/vitest.d.ts` file - Import `vitest/globals` from any `.ts` file (e.g., your [setup file](#setupfiles) or a `.d.ts` file). 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. + Create a `@types/vitest.d.ts` file in your project root that imports `vitest/globals`: ```ts - // test-setup.ts or vitest.d.ts import 'vitest/globals' ``` -2. Configure it in [`tsconfig.json#compilerOptions.types`](https://www.typescriptlang.org/tsconfig/#types) +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' + ``` + +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`.