执行 mochawesome-merge 时出现“JSON 输入意外结束”
Posted
技术标签:
【中文标题】执行 mochawesome-merge 时出现“JSON 输入意外结束”【英文标题】:"Unexpected end of JSON input" while doing mochawesome-merge 【发布时间】:2021-11-15 05:34:44 【问题描述】:我尝试按照this 帖子中的建议删除命令中的空格。我还尝试按照this 帖子中的建议重新编码 mochawesome.json。但没有工作。
您可以在 gdrive 上找到我的 json 文件 here。
当我尝试合并它们时,出现以下错误:
npx mochawesome-merge cypress/reports/test/test/*.json>cypress/reports/test/test/merged_report.json
Debugger attached.
Debugger attached.
ERROR: Failed to merge reports
SyntaxError: cypress/reports/test/test/merged_report.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at F:\myproj-testing\node_modules\mochawesome-merge\node_modules\jsonfile\index.js:33:18
at F:\myproj-testing\node_modules\graceful-fs\graceful-fs.js:123:16
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...
我使用的是 Windows 10。以下是节点相关版本:
>node --version
v14.16.0
> npm --version
Debugger attached.
6.14.11
package.json
"name": "myproj-testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"merge":"mochawesome-merge cypress/reports/*.json>cypress/reports/merged_report.json",
"generate_mochawesome_report": "marge cypress/reports/merged_report.json --reportDir ./ --inline",
"final_report": "npm run merge && npm run generate_mochawesome_report",
"generate_report": "python cypress/utils/merge.py && npm run generate_mochawesome_report"
,
"keywords": [],
"author": "",
"license": "ISC",
"dependencies":
"cypress": "^8.3.0",
"cypress-file-upload": "^5.0.8"
,
"devDependencies":
"mocha": "^9.1.1",
"mochawesome": "^6.2.2",
"mochawesome-merge": "^4.2.0"
更新
我尝试只运行mochawesome-merge cypress/reports/*.json
,它在控制台上打印了合并的 json。所以我尝试先将合并的 json 字符串通过管道传输到.txt
文件,然后将.txt
文件重命名为.json
文件,最后执行marge
。奇怪的是它开始工作了......所以这些是我更新的package.json
脚本:
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"merge":"mochawesome-merge cypress/reports/*.json>cypress/reports/merged_report.json",
"mergetotxt":"mochawesome-merge cypress/reports/*.json > cypress/reports/merged_report.txt",
"generate_mochawesome_report": "marge cypress/reports/merged_report.json --reportDir ./ --inline",
"final_report": "npm run merge && npm run generate_mochawesome_report",
"final_report2": "npm run mergetotxt && rename cypress\\reports\\merged_report.txt merged_report.json && npm run generate_mochawesome_report",
"generate_report": "python cypress/utils/merge.py && npm run generate_mochawesome_report"
注意以下两个新脚本:
"mergetotxt":"mochawesome-merge cypress/reports/*.json > cypress/reports/merged_report.txt",
"final_report2": "npm run mergetotxt && rename cypress\\reports\\merged_report.txt merged_report.json && npm run generate_mochawesome_report",
运行npm run final_report2
会生成所需的报告。
【问题讨论】:
请添加您的package.json
添加了 package.json
能否在>
、"merge":"mochawesome-merge cypress/reports/*.json > cypress/reports/merged_report.json",
前后添加空格,然后尝试运行npm run merge
。单独运行命令。
嗯,最初我只尝试了空格。它没有用,所以我尝试按照this 帖子中的建议删除空格。我也刚试了一次。没有运气。顺便说一句,我尝试只运行mochawesome-merge cypress/reports/*.json
,它会在控制台上打印合并的 json。因此,我尝试先将合并的 json 字符串通过管道传输到 .txt
文件,然后将 .txt
文件重命名为 .json
文件,最后执行 marge
。奇怪的是它开始工作了...打算将此添加到帖子中
虽然这里的很多信息似乎与错误实际上并不相关:错误是关于来自特定文件的不完整 JSON:显示 npx mochawesome-merge cypress/reports/test/test/*.json
的输出在您的系统而不将其定向到文件中,以确保它不包含有用的标准输出控制台日志之类的内容,例如“开始运行...”或“合并完成...”等。
【参考方案1】:
问题是这一行:
"merge":"mochawesome-merge cypress/reports/*.json>cypress/reports/merged_report.json"
您不能为结果 json 使用路径。
"merge":"mochawesome-merge cypress/reports/*.json > merged_report.json"
应该工作
【讨论】:
【参考方案2】:请试试这个“合并报告”:“mochawesome-merge cypress/reports/*.json > merge_report.txt > merge_report.json”,
这对我有用。
【讨论】:
【参考方案3】:"merge":"mochawesome-merge cypress/reports/*.json>cypress/reports/output.json",
"generate_mochawesome_report": "marge cypress/reports/output.json --reportDir ./ --inline",
"final_report": "npm run merge && npm run generate_mochawesome_report"
试试这个方法,应该可以的...
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于执行 mochawesome-merge 时出现“JSON 输入意外结束”的主要内容,如果未能解决你的问题,请参考以下文章
Cypress web自动化26-mochawesome-merge合并json报告