我正在编写一个mocha测试记者,我想用它来编写自定义的Cypress测试文档。哪种方法是调试报表代码的正确方法(也许使用intellij Idea)?
编辑我尝试使用intellij Idea工具进行调试,在调试模式下运行cypress (打开和运行)。我还尝试了允许测试调试的IntelliJ Cypress plugin pro版本。
我不能止步于断点。
所以我至少尝试打印一些调试日志,但是我看不到我的日志。
发布于 2021-06-08 03:54:44
我不能让它在Cypress上工作,但我可以在VSCode中使用Mocha。
调试步骤:
为您的项目安装ts-
npm i ts-node typescript --save-devsrc文件夹中使用以下内容创建自定义报告.ts:(摘自https://mochajs.org/api/tutorial-custom-reporter.html,稍作修改)import { reporters, Runner, Suite, Test } from 'mocha';
const { EVENT_RUN_BEGIN, EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_SUITE_BEGIN, EVENT_SUITE_END } = Runner.constants;
// This reporter outputs test results, indenting two spaces per suite
export class CustomReporter extends reporters.Base {
private indents = 0;
constructor(runner: Runner) {
super(runner);
const stats = runner.stats;
runner
.once(EVENT_RUN_BEGIN, () => {
console.info('start');
})
.on(EVENT_SUITE_BEGIN, (suite: Suite) => {
this.increaseIndent();
})
.on(EVENT_SUITE_END, (suite: Suite) => {
this.decreaseIndent();
})
.on(EVENT_TEST_PASS, (test: Test) => {
// Test#fullTitle() returns the suite name(s)
// prepended to the test title
console.log(`${this.indent()}pass: ${test.fullTitle()}`);
})
.on(EVENT_TEST_FAIL, (test: Test, err: any) => {
console.log(`${this.indent()}fail: ${test.fullTitle()} - error: ${err.message}`);
})
.once(EVENT_RUN_END, () => {
console.log(`end: ${stats.passes}/${stats.passes + stats.failures} ok`);
});
}
private indent() {
return Array(this.indents).join(' ');
}
private increaseIndent() {
this.indents++;
}
private decreaseIndent() {
this.indents--;
}
}index.js,并按如下方式导出我们的报告器: module.exports = require("./src/custom-reporter").CustomReporter;package.json中: "scripts": {
"test": "mocha -r ts-node/register specs/*.spec.ts --reporter index"
},.vscode/launch.json,并添加以下代码:{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Mocha tests",
"cwd": "${workspaceRoot}",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-r",
"ts-node/register",
"--reporter",
"index",
"${workspaceFolder}/specs/**/*.spec.ts"
],
"protocol": "inspector",
"sourceMaps": true,
"console": "integratedTerminal"
},
]
}src/custom-reporter.ts中的
Run and Debug面板(Ctrl+Shift+D),选择Debug Mocha tests并按play按钮这样,您应该能够启动测试运行并命中VSCode中的断点。
干杯!
https://stackoverflow.com/questions/64755044
复制相似问题