Jenkins执行Playwright脚本报错:test.describe()调用位置异常
问题:Playwright脚本本地正常但Jenkins执行报错
本地运行Playwright脚本无任何问题,但在Jenkins环境执行时遭遇报错,报错信息如下:
+ npx playwright test <------My ENV---->prod Error: Playwright Test did not expect test.describe() to be called here. Most common reasons include: - You are calling test.describe() in a configuration file. - You are calling test.describe() in a file that is imported by the configuration file. - You have two different versions of @playwright/test. This usually happens when one of the dependencies in your package.json depends on @playwright/test. at cgt_crossroad_validation\tests\CrossRoad_AIR.spec.js:11 9 | let loginClass 10 | > 11 | test.describe('Validation Catering invoice processing', () => { | ^ 12 | 13 | test.beforeEach(async ({browser }, testInfo) => { 14 | page = await browser.newPage(); at TestTypeImpl._currentSuite (E:\.jenkins\workspace\CrossRoad_Validation\cgt_crossroad_validation\node_modules\playwright\lib\common\testType.js:71:13) at TestTypeImpl._describe (E:\.jenkins\workspace\CrossRoad_Validation\cgt_crossroad_validation\node_modules\playwright\lib\common\testType.js:104:24) at Function.describe (E:\.jenkins\workspace\CrossRoad_Validation\cgt_crossroad_validation\node_modules\playwright\lib\transform\transform.js:256:12) at Object.<anonymous> (E:\.jenkins\workspace\CrossRoad_Validation\cgt_crossroad_validation\tests\CrossRoad_AIR.spec.js:11:6)
当前Jenkinsfile配置:
stages { stage("checkout") { steps { checkoutRepo(branch, project, cgtx_repo) } } // stage('Install Playwright') { // steps { // sh 'npm install -g playwright' // } // } stage('Install dependencies') { steps { dir('cgt_crossroad_validation') { // Execute npm install within the changed directory sh 'npm install' } } } stage('Run Playwright Tests') { steps { sh 'npx playwright test' } } }
无论是否执行npm install,报错一致,以下是解决方法:
解决方法
1. 修正Jenkins测试执行路径
当前依赖安装在cgt_crossroad_validation目录,但测试命令在根目录执行,会导致调用全局或其他目录的Playwright版本。修改Jenkinsfile的测试阶段,切换到对应目录执行:
stage('Run Playwright Tests') { steps { dir('cgt_crossroad_validation') { sh 'npx playwright test' } } }
2. 排查并统一@playwright/test版本
- 本地和Jenkins环境分别执行
npm list @playwright/test,确认版本是否一致 - 执行
npm ls @playwright/test排查是否有依赖引入了不同版本的@playwright/test - 在
package.json中添加resolutions字段锁定版本(替换为你本地正常运行的版本):"resolutions": { "@playwright/test": "1.40.0" } - 重新执行
npm install
3. 检查配置文件是否误导入测试文件
- 打开
playwright.config.js(或.ts),确认没有直接或间接导入测试文件(如CrossRoad_AIR.spec.js) - 检查配置文件中引用的其他模块,确保它们未导入测试文件
4. 清理Jenkins工作空间缓存
在Jenkins任务的checkout阶段前添加清理步骤,避免旧依赖残留:
stage("checkout") { steps { deleteDir() checkoutRepo(branch, project, cgtx_repo) } }
内容的提问来源于stack exchange,提问作者Daniel Tan
相关产品推荐
相关产品推荐

