@ codeceptjs/ 反应
基于AST的反射,用于CodeceptJS测试,套房,和台阶. 安全读取和编辑情景源而不使用regex.
安装
npm install @codeceptjs/reflection
为什么
CodeceptJS测试跑者,治疗者,AI代理,以及MCP服务器需要读取并重写情景源代码. 用Regex来做是脆弱的和不安全的。 这个软件包给了您一个原则性的,AST驱动的API,以便在它的源文件中定位一个步骤,测试,或套件,读取精确范围,并替换它同时保存格式化,注释和元数据.
快开始
import { Reflection } from '@codeceptjs/reflection'
// You have a live CodeceptJS test object (from event.test.before, event.test.after, etc.)
const tr = Reflection.forTest(test)
console.log(tr.read()) // full Scenario(...) source
console.log(tr.fileName) // path to the source file
// Build an edit — nothing is written yet
const edit = tr.replace(`Scenario('login works', async ({ I }) => { I.amOnPage('/') })`)
console.log(edit.diff()) // unified diff preview
console.log(edit.preview()) // full new source
edit.apply() // atomic write to disk
反射类
Reflection.forStep(step, { test }) // StepReflection
Reflection.forTest(test) // TestReflection
Reflection.forSuite(suite) // SuiteReflection — addTest/removeTest
Reflection.forPageObject(filePath, { name }) // PageObjectReflection — add/replace/remove members
Reflection.project('./codecept.conf.js') // ProjectReflection — enumerate suites, tests, steps, page objects
每个 forX 反射器暴露出相同的形状 : fileName, (中文(简体) ), read(), (中文(简体) ), replace(),加特定类型存取器. SuiteReflection 财务报告和审定财务报表 PageObjectReflection 还支持方案增加/撤走儿童.
Reflection.project(...) 读取一个 CodeceptJS 配置并给出整个工程的地图——然后将单个目标交给专门的反射器.
击打
在一个文件中替换多个事项, 一个写 :
const batch = Reflection.batch(filePath)
batch.add(sr1.replace('I.click("a")'))
batch.add(sr2.replace('I.click("b")'))
batch.apply()
文档
所需资源
- 节点.js 18+
- CodeceptJS 3.x或 4.x(可选对等点——仅在消费者运行时需要)
typescript软件包(可选的对等点——只有在您反映时才需要).ts编号 :.tsx文档)
许可证
麻省理工学院