Skip to content

Commit

Permalink
Merge pull request #580 from lxKylin/api-index
Browse files Browse the repository at this point in the history
docs: update content
  • Loading branch information
thinkasany authored Jan 8, 2025
2 parents 161f205 + 2e990ca commit 7a7bfdf
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions advanced/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
title: Advanced API
---

# Getting Started
# 快速起步

::: warning
This guide lists advanced APIs to run tests via a Node.js script. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
本指南列出了通过 Node.js 脚本运行测试的高级 API。如果你只是想[运行测试](/guide/),你可能不需要这些内容。这些 API 主要用于库作者。
:::

You can import any method from the `vitest/node` entry-point.
你可以从 `vitest/node` 入口点导入任何方法。

## startVitest

Expand All @@ -22,7 +22,7 @@ function startVitest(
): Promise<Vitest>
```

You can start running Vitest tests using its Node API:
你可以使用 Vitest Node API 开始运行测试:

```js
import { startVitest } from 'vitest/node'
Expand All @@ -32,17 +32,17 @@ const vitest = await startVitest('test')
await vitest.close()
```

`startVitest` function returns [`Vitest`](/advanced/api/vitest) instance if tests can be started.
`startVitest` 函数如果可以启动测试,将返回一个 [`Vitest`](/advanced/api/vitest) 实例。

If watch mode is not enabled, Vitest will call `close` method automatically.
如果未启用监视模式,Vitest 将自动调用 `close` 方法。

If watch mode is enabled and the terminal supports TTY, Vitest will register console shortcuts.
如果启用了监视模式且终端支持 TTYVitest 将注册控制台快捷键。

You can pass down a list of filters as a second argument. Vitest will run only tests that contain at least one of the passed-down strings in their file path.
你可以将过滤器列表作为第二个参数传递。Vitest 将仅运行文件路径中包含至少一个传递字符串的测试。

Additionally, you can use the third argument to pass in CLI arguments, which will override any test config options. Alternatively, you can pass in the complete Vite config as the fourth argument, which will take precedence over any other user-defined options.
此外,你可以使用第三个参数传递 CLI 参数,这些参数将覆盖任何测试配置选项。或者,你可以将完整的 Vite 配置作为第四个参数传递,这将优先于任何其他用户定义的选项。

After running the tests, you can get the results from the [`state.getTestModules`](/advanced/api/test-module) API:
运行测试后,你可以从 [`state.getTestModules`](/advanced/api/test-module) API 获取结果:

```ts
import type { TestModule } from 'vitest/node'
Expand All @@ -53,7 +53,7 @@ console.log(vitest.state.getTestModules()) // [TestModule]
```

::: tip
The ["Running Tests"](/advanced/guide/tests#startvitest) guide has a usage example.
[“运行测试”](/advanced/guide/tests#startvitest) 指南中有使用示例。
:::

## createVitest
Expand All @@ -67,7 +67,7 @@ function createVitest(
): Promise<Vitest>
```

You can create Vitest instance by using `createVitest` function. It returns the same [`Vitest`](/advanced/api/vitest) instance as `startVitest`, but it doesn't start tests and doesn't validate installed packages.
你可以使用 `createVitest` 函数创建一个 Vitest 实例。它返回与 `startVitest` 相同的 [`Vitest`](/advanced/api/vitest) 实例,但不会启动测试也不会验证已安装的包。

```js
import { createVitest } from 'vitest/node'
Expand All @@ -78,7 +78,7 @@ const vitest = await createVitest('test', {
```

::: tip
The ["Running Tests"](/advanced/guide/tests#createvitest) guide has a usage example.
[“运行测试”](/advanced/guide/tests#createvitest) 指南中有使用示例。
:::

## resolveConfig
Expand All @@ -93,7 +93,7 @@ function resolveConfig(
}>
```

This method resolves the config with custom parameters. If no parameters are given, the `root` will be `process.cwd()`.
此方法使用自定义参数解析配置。如果没有提供参数,则 `root` 将为 `process.cwd()`

```ts
import { resolveConfig } from 'vitest/node'
Expand All @@ -113,15 +113,15 @@ const { vitestConfig, viteConfig } = await resolveConfig({
```

::: info
Due to how Vite's `createServer` works, Vitest has to resolve the config during the plugin's `configResolve` hook. Therefore, this method is not actually used internally and is exposed exclusively as a public API.
由于 Vite`createServer` 工作方式,Vitest 必须在插件的 `configResolve` 钩子中解析配置。因此,此方法实际上并未在内部使用,而是仅作为公共 API 暴露。

If you pass down the config to the `startVitest` or `createVitest` APIs, Vitest will still resolve the config again.
如果你将配置传递给 `startVitest` `createVitest` APIVitest 仍然会重新解析配置。
:::

::: warning
The `resolveConfig` doesn't resolve the `workspace`. To resolve workspace configs, Vitest needs an established Vite server.
`resolveConfig` 不会解析 `workspace`。要解析工作区配置,Vitest 需要一个已建立的 Vite 服务器。

Also note that `viteConfig.test` will not be fully resolved. If you need Vitest config, use `vitestConfig` instead.
另外请注意,`viteConfig.test` 不会被完全解析。如果你需要 Vitest 配置,请使用 `vitestConfig` 代替。
:::

## parseCLI
Expand All @@ -133,7 +133,7 @@ function parseCLI(argv: string | string[], config: CliParseOptions = {}): {
}
```

You can use this method to parse CLI arguments. It accepts a string (where arguments are split by a single space) or a strings array of CLI arguments in the same format that Vitest CLI uses. It returns a filter and `options` that you can later pass down to `createVitest` or `startVitest` methods.
你可以使用此方法来解析 CLI 参数。它接受一个字符串(其中参数以单个空格分隔)或一个与 Vitest CLI 使用的格式相同的 CLI 参数字符串数组。它返回一个过滤器和 `options`,你可以在稍后传递给 `createVitest` `startVitest` 方法。

```ts
import { parseCLI } from 'vitest/node'
Expand Down

0 comments on commit 7a7bfdf

Please sign in to comment.