自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兩個(gè) Prompt 實(shí)現(xiàn) Cursor 批量生成單測(cè)

開(kāi)發(fā) 前端
打開(kāi) Cursor Agent Yolo 模式,它能自動(dòng)調(diào)用各類 cli 工具,驗(yàn)證生成的代碼是否符合預(yù)期,比如自動(dòng)調(diào)用 lint 檢查代碼風(fēng)格,ts 檢查類型合規(guī),vitest 檢測(cè)單測(cè)是否通過(guò)等。

最近用 Cursor 生成了一批單測(cè)代碼,合計(jì) 1.1w 行代碼,最開(kāi)始生成的效果并不好,經(jīng)過(guò)一番調(diào)教,目前我所維護(hù)的倉(cāng)庫(kù)已經(jīng)實(shí)現(xiàn)某種半自動(dòng)化的 UT 生成能力:在我并不理解代碼邏輯的情況下,僅需一行 Prompt ,即可為 Monorepo 中某個(gè) Package 所有源碼批量生成單測(cè)的效果:

圖片圖片

圖片圖片

這里面 Cursor 逐次做了幾件事情:

  • 根據(jù)包名找到源碼目錄;
  • 掃描源碼目錄,確定需要生成單測(cè)的源碼文件,并記錄到 .cursor/task.md 文件中;

圖片圖片

  • 針對(duì) .cursor/task.md 標(biāo)記的每一個(gè)任務(wù),根據(jù) .cursor/rules/instruct-ut.mdc 指令迭代生成單測(cè)代碼,直至測(cè)試通過(guò)且覆蓋率達(dá)標(biāo)。

這是怎么做到的呢?PE!純粹的PE,沒(méi)有增加任何一行代碼!

Prompt 解析

首先,Cursor 支持多文件編輯,支持倉(cāng)庫(kù)索引,支持配置 .cursorrules 等特性,因此其本身就有能力批量完成編程任務(wù),但在過(guò)往的測(cè)試中,批量生成單測(cè)的效果并不好,遇到過(guò)不少問(wèn)題,主要可歸結(jié)為兩個(gè)點(diǎn):

  1. Package 的文件可能很多,Cursor Composer 經(jīng)常生成一部分文件后就會(huì)退出生成過(guò)程,只能部分完成任務(wù);
  2. 我所維護(hù)的是一個(gè) Monorepo 倉(cāng)庫(kù),體積很大,合計(jì) 300+ Package,200w 行代碼,代碼的嵌套層次很深,模塊依賴關(guān)系非常復(fù)雜,并且大量使用 TS Alias 等特性,導(dǎo)致單測(cè)的復(fù)雜度也相應(yīng)增加了許多;

針對(duì)第二點(diǎn),實(shí)測(cè)只需補(bǔ)充若干 Prompt ,用于明確單測(cè)代碼規(guī)范、技術(shù)棧等關(guān)鍵上下文信息即可(.cursor/rules/spec-for-ut.mdc):

# Unit Test Specification Expert

## Skills
1. Writing unit tests for React components and functions using Vitest.
2. Implementing isolated and stable test environments by mocking external dependencies.
3. Structuring test code following the arrange-act-assert pattern.
4. Ensuring compliance with file placement conventions for test files.
5. Avoiding source code modifications solely for unit testing purposes.

## Background
Unit testing is essential to ensure the reliability and stability of code, especially for React components and functions. Adhering to best practices and ensuring high test coverage is critical to maintaining a robust codebase.

## Goals
1. Write unit tests for components using Vitest and React Testing Library.
2. Implement integration tests for critical user flows.
3. Maintain unit test coverage of at least 80%.
4. Ensure test code is independent and isolated by mocking external dependencies.
5. Follow test file placement conventions to organize code effectively.
6. Avoid modifying source code for testing purposes.

## Rules
1. Use Vitest exclusively for unit testing; avoid Jest.
2. Use snapshot testing judiciously and only where appropriate.
3. Ensure test files are placed in the correct `__tests__` directory structure:
   - Example 1: The unit test file for `root/packages/community/pages/src/bot/components/bot-store-chat-area-provider/index.tsx` should be placed in `root/packages/community/pages/__tests__/bot/components/bot-store-chat-area-provider/index.test.tsx`.
   - Example 2: The unit test file for `root/xxx/src/foo/bar/xxx.ts` should be placed in `root/xxx/__tests__/foo/bar/xxx.test.tsx`.
4. Follow the arrange-act-assert pattern in test code organization.
5. Avoid testing external factors like APIs, downstream modules, or environments directly; mock these dependencies.
6. Use `expect(ele?.className).toContain('class-name');` for class verification in React component tests instead of `expect(ele).toHaveClass('class-name');`.
7. Maintain English usage throughout - avoid Chinese comments, test case names, etc.

# Reference

If necessary, you can refer to the following documents:

- [Vitest](https://vitest.dev/guide/)
- [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/)
- [Arrange-Act-Assert](https://github.com/civic/arrange-act-assert)

難點(diǎn)在于第一點(diǎn):如何持續(xù)性地生成代碼?這里取了個(gè)巧,讓 Cursor 在將任務(wù)進(jìn)度記錄到外部文件中(.cursor/rules/instruct-ut.mdc):

# Instructions

When a user requests to generate unit tests for a directory of packages, first check the task progress in the `.cursor/unit-test` directory and prioritize resuming the previous progress. If no previous progress is recorded, start a new task and execute:

1. Find the code location corresponding to the package name;
2. Locate the source files containing logic code in the directory, record them as a task list, and save to `.cursor/unit-test/tasks.md`, for example:

- [ ] `src/index.ts`
- [ ] `src/utils/index.ts`
- [ ] `src/hooks/index.ts`
- [ ] `src/components/index.ts`
- [ ] `src/pages/index.ts`
- [ ] `src/services/index.ts`
- [ ] `src/types/index.ts`

3. Following the instructions in `./.cursor/spec-for-ut.md`, generate unit tests for each file from the previous step. After generation and passing tests, record in `.cursor/unit-test/tasks.md`, for example:

- [x] `src/index.ts`
- [x] `src/utils/index.ts`
- [x] `src/hooks/index.ts`
- [x] `src/components/index.ts`
- [x] `src/pages/index.ts`
- [x] `src/services/index.ts`
- [x] `src/types/index.ts`

4. If some unit test files consistently fail and only have one or two failing test cases, remove those test cases to ensure overall test passing

5. If any error cases are encountered during generation, record them in `.cursor/lessons` to avoid repeating the same mistakes in the future;

6. Recursively generate unit tests for each file until all files are completed, then exit the execution process

7. after all the files are completed, you should update the `.cursor/unit-test/tasks.md` file to reflect the progress.

# Limitations

- you can just use vitest to generate the unit test code.

之后,Cursor 每次生成單測(cè)時(shí)都會(huì)先到 tasks.md 文件中查找任務(wù)列表,恢復(fù)上次執(zhí)行進(jìn)度。

效果

在上述 Cursor Rules 基礎(chǔ)上,后續(xù)的交互就簡(jiǎn)單許多了,只需使用下述 Prompt 即可:

為 @coze/api 包生成單測(cè)

//or 

為 xxx 目錄/文件生成單測(cè)

至此,至少?gòu)摹傲俊鄙?,Cursor 已經(jīng)能幫助生成比較完整的單測(cè)代碼了,并且大部分簡(jiǎn)單代碼都能一次性通過(guò)。但對(duì)于復(fù)雜場(chǎng)景、復(fù)雜組件,生成的代碼通常會(huì)存在一些問(wèn)題,沒(méi)法直接跑通,例如:

圖片圖片

圖片圖片

接下來(lái)就需要人工介入修復(fù)這些疑難雜癥了,有一些小技巧:

  • 使用 測(cè)試框架(vitest/jest 都有提供) 的 [only](https://vitest.dev/api/#describe-only) 接口配合 Filter 能力,只跑存在問(wèn)題的用例,降低信息噪音;
  • 可以使用terminal 右上角的 Add to Composer 按鈕,讓 LLM 繼續(xù)幫你解決問(wèn)題;

圖片圖片

  • 其次,絕大部分問(wèn)題都出在 Mock 上面,例如下圖所示,錯(cuò)誤看起來(lái)是 ESM 與 CommonJS 互相調(diào)用的問(wèn)題,實(shí)則可以通過(guò) Mock 解決,因此需要有意識(shí) Mock 掉所有下游模塊調(diào)用、環(huán)境變量調(diào)用等(LLM 在這方面做的并不是很好,還是需要比較多人工介入);

圖片圖片

最后

為免遺漏,最后在總結(jié)幾個(gè)必要條件:

  • 安裝 Cursor,盡可能充上會(huì)員;
  • 開(kāi)啟 Codebase Indexing;
  • 打開(kāi) Cursor Agent Yolo 模式,它能自動(dòng)調(diào)用各類 cli 工具,驗(yàn)證生成的代碼是否符合預(yù)期,比如自動(dòng)調(diào)用 lint 檢查代碼風(fēng)格,ts 檢查類型合規(guī),vitest 檢測(cè)單測(cè)是否通過(guò)等;

圖片圖片

  • 把上述兩段 Rules 加進(jìn) .cursor/rules 中。

其次,面對(duì)復(fù)雜代碼時(shí),LLM 通常無(wú)法順利生成對(duì)應(yīng)單測(cè),甚至在人類智能介入的情況下,成本依然很高,因此更建議盡可能保持源碼的簡(jiǎn)潔性,遵循一些最佳實(shí)踐規(guī)則。

責(zé)任編輯:武曉燕 來(lái)源: Tecvan
相關(guān)推薦

2024-12-27 09:05:18

2016-03-31 11:28:21

imageView圖片輪播

2010-07-17 00:50:12

batch Telne

2010-09-09 15:23:16

SQL更新數(shù)據(jù)

2021-09-08 09:52:34

語(yǔ)言

2022-06-17 09:46:51

Chrome 102Chrome瀏覽器

2023-04-06 08:03:43

Spock插件Surefire

2010-05-27 09:50:18

MySQL導(dǎo)入sql腳

2011-09-07 16:43:38

Qt Widget

2009-06-30 09:37:02

對(duì)象比較Java

2010-09-13 16:55:27

DIV橫向排列

2010-07-21 11:32:35

SQL Server日

2020-11-13 07:16:09

線程互斥鎖死循環(huán)

2020-10-26 08:19:53

算法隊(duì)列

2009-07-16 10:39:00

SwingUtilit

2010-09-10 15:26:05

SOAP封裝

2025-03-28 11:09:04

2009-07-15 18:29:22

Jython應(yīng)用

2021-08-03 08:13:47

數(shù)據(jù)

2017-01-15 01:45:37

簡(jiǎn)歷簡(jiǎn)歷模板數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)