SuperAPI AI Directory

test-skill

Testing & quality

View repository

Updated 10-15 23:48

About this skill

When to Use This Skill

When the user needs to write a unit test, trigger this skill to write a unit test.

Test file location

packages tests

Tests in packages are written under the FastGPT/packages/xxx/test directory, and the subpaths correspond to the directory structure of packages. For example:

packages/global/common/error/s3.tsfile, the corresponding test case file path is packages/test/global/common/error/s3.test.ts。

Also, files in packages can be imported via @fastgpt. For example:

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

projects tests

Tests in projects are written under the FastGPT/projects/app/test directory, and the subpaths correspond to the directory structure of projects.

projects/app/src/pages/api/core/dataset/collection/create.tsfile, the corresponding test case file path is projects/app/test/api/core/dataset/collection/create.test.ts。

Test file rules

General rules

  1. Each file corresponds to one test file. Each function corresponds to a different describe block.
  2. 100% line and branch coverage is required.
  3. Test files should avoid introducing third-party dependency libraries as much as possible, and use more native methods for checking. If a third-party dependency library needs to be introduced, export the dependency library from the corresponding file for the test to use. For example:
// 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. Minimize function mocks. If it is a function that can run natively on the system, there is no need to mock it; only mock dependencies that cannot run directly locally (such as those requiring remote services, API keys, etc.).
  2. For type.ts, constants.ts, schema.ts, *.schema.ts files, as well as static data, skip and ignore them directly.
  3. According to the vitest.config.mts file configuration, skip files that do not need testing.
  4. Mock.ts file contains the global mock content. When writing tests, do not mock repeatedly. In theory, tests can mock and run various infra.

Basic function file tests

Try not to mock, but run its logic completely for testing.

Functions with API requests

Mock the corresponding API request for testing.

Writing process

1. Task preparation

  1. Obtain the test files that need to be written.
  2. Create a task list to complete the test case writing for each file one by one.

2. Test case writing

Different test case files can be written in parallel.

  1. Check the corresponding .test.ts test file; if it does not exist, create it.
  2. Think about and analyze the code, then write test cases.
  3. Check for TS errors to ensure there are no ts errors.
  4. Complete the writing of all test files

3. Result verification

  1. Callpnpm test <file-path> <test-name>to run tests and check coverage, ensuring that the coverage of each file reaches 90% or above.
  2. If the test does not pass, check the code logic or test cases based on the error information.
  3. If a second modification is needed, return to "2. Test case writing".

What scenarios unit tests include

  1. Basic scenarios
  2. Complex scenarios
  3. Boundary values
  4. Security boundary cases (infinite loops, system crashes, extremely large data, etc.)
  5. Exception scenarios

Common commands

# 运行所有测试
pnpm test

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

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