Skip to content
cccskills
FreePublished on GitHub

test-case

Write unit tests for FastGPT code, covering normal behavior, edge cases, and errors. It guides test placement, limits mocks that replace real dependencies, and checks TypeScript errors and coverage, the share of code exercised by tests.

OriginalShow the original description

当用户需要编写一个单元测试时,触发该 skill,编写单元测试。

See how to install

Good for

  • Adding unit tests for individual functions
  • Testing edge cases and error handling
  • Testing functions that make API requests
  • Running selected tests and checking coverage

Files included (1)

  • SKILL.md3.4 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

When to Use This Skill

用户需要编写一个单元测试时,触发该 skill,编写单元测试。

测试文件位置

packages 测试

packages 里的测试,写在 FastGPT/packages/xxx/test 目录下,子路径对应 packages 的目录结构。例如:

packages/global/common/error/s3.ts文件,对应的测例文件路径为 packages/test/global/common/error/s3.test.ts。

并且,可以通过 @fastgpt 来导入 packages 里的文件。 例如:

import { s3 } from '@fastgpt/global/common/error/s3';

projects 测试

projects 里的测试,写在 FastGPT/projects/app/test 目录下,子路径对应 projects 的目录结构。

projects/app/src/pages/api/core/dataset/collection/create.ts文件,对应的测例文件路径为 projects/app/test/api/core/dataset/collection/create.test.ts。

测试文件规则

通用规则

  1. 每个文件,对应一个测试文件。每个函数对应不同的 describe 块。
  2. 需覆盖 100% 行数和分支。
  3. 测试文件尽可能不要引入第三方依赖库,使用较为原生的方式进行检查。如果需要引入第三方依赖库,则从对应文件里 export 依赖库给 test 使用。例如:
// FastGPT/packages/service/common/geo/index.ts
import type { NextApiRequest } from 'next';
// 同时导出一个依赖给 FastGPT/packages/service/test/common/geo/index.test.ts 使用
export type { NextApiRequest } from 'next';
  1. 尽量减少函数 mock,如果是系统上原生可运行的函数,则无需 mock,只需要 mock 那些无法本地直接运行的依赖(比如需要远程服务,API 密钥之类的)
  2. 对于 type.ts, constants.ts, schema.ts, *.schema.ts 文件,以及静态数据,直接跳过忽略。
  3. 根据 vitest.config.mts 文件配置,跳过不需要测试的文件。
  4. Mock.ts 文件里,包含了全局 mock 的内容,在编写测试时,请勿重复 mock。理论上,测试里可以 mock 运行各类 infra。

基础函数文件测试

尽量不要 mock,而是完整的运行其逻辑进行测试。

带 API 请求的函数

mock 对应的 API 请求进行测试。

编写流程

一、任务准备

  1. 获取所需要编写的测试文件。
  2. 创建任务清单,来逐个完成每个文件的测例编写。

二、测例编写

不同测例文件,可以并行进行编写。

  1. 检查对应的 .test.ts 测试文件,如果没有则创建。
  2. 思考和分析代码后编写测试样例。
  3. 检查 TS 错误,确保无 ts 报错。
  4. 完成所有测试文件编写

三、结果验证

  1. 调用pnpm test <file-path> <test-name>来运行测试并检查覆盖率,确保每个文件的覆盖率达到 90% 以上。
  2. 如果测试不通过,则根据错误信息检查代码逻辑或者测试用例。
  3. 如需二次修改,则回到”二、测例编写“。

单测包含哪些场景

  1. 基础场景
  2. 复杂场景
  3. 边界值
  4. 安全边界情况(死循环、系统崩溃、超大数据等)
  5. 异常场景

常用命令

# 运行所有测试
pnpm test

# 运行指定测试文件(file-path 填完整文件路径)
pnpm test <file-path>

# 运行指定测试文件的指定测试
pnpm test <file-path> <test-name>

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

add-permission

FreeSummary

Integrate new FastGPT resources, such as AgentSkill or Plugin, with permission management. Choose an integration guide based on resource hierarchy and permission inheritance, locate the relevant code, and review a checklist for missing implementation steps.

  • Adding permission support to a new FastGPT resource
  • Choosing an integration path based on permission inheritance
  • Finding code locations for permission definitions and checks
labring/FastGPT29.9KUpdated Oct 19, 2026

api-development

FreeSummary

Develop and review FastGPT APIs with consistent input and output checks, TypeScript types, and OpenAPI documentation. It guides the use of Zod schemas, definitions that describe and validate data, alongside permission checks and reusable structures.

  • Adding a new FastGPT API route
  • Changing API inputs or responses
  • Reviewing API validation and documentation
labring/FastGPT29.9KUpdated Oct 19, 2026

ci-workflow-sync

FreeSummary

Keep FastGPT build and test workflows aligned across GitHub Actions and Forgejo Actions. Adapt registry destinations, credentials, proxy settings, and platform-specific steps so the corresponding workflows support the internal network environment.

  • Synchronizing CI changes across GitHub and Forgejo
  • Adapting image builds for an internal network
  • Checking pull requests for missing workflow updates
labring/FastGPT29.9KUpdated Oct 19, 2026

Retire a FastGPT workflow node while preserving existing workflows. The skill guides changes to templates, execution handlers, and interface references so the node disappears from creation menus but still loads, runs, and displays a replacement notice.

  • Retiring a FastGPT workflow node
  • Hiding a node from creation menus
  • Preserving existing workflows during node retirement
labring/FastGPT29.9KUpdated Oct 19, 2026

doc-i18n

FreeSummary

Translate FastGPT documentation from Chinese into natural English for North American readers. Follow the shared product glossary, preserve document structure and code, synchronize navigation files, and check for missing translations or English pages that need updates.

  • Updating English docs after changes to Chinese pages
  • Translating a specified documentation folder
  • Synchronizing English navigation with translated pages
labring/FastGPT29.9KUpdated Oct 19, 2026

Deploy FastGPT on your own server using its official installation script and Docker Compose, which runs its services together. Check service health and access, investigate deployment problems, and receive the access URL, initial login details, and next steps.

  • Setting up FastGPT on your own server
  • Checking service health after deployment
  • Investigating port conflicts or container startup failures
labring/FastGPT29.9KUpdated Oct 19, 2026

See all skills by labring

Report a problem with this skill