testing - 如何调试自定义 mocha 测试报告器?
问题描述
我正在编写一个 mocha 测试报告器,我想将它用于定制的 Cypress 测试文档。哪种是调试报告代码的正确方法(可能使用 intellij Idea)?
编辑我尝试使用 intellij Idea 工具进行调试,在调试模式下运行 cypress(打开和运行)。我还尝试了允许测试调试的 IntelliJ Cypress 插件专业版。
我不能在断点处停下来。
所以我至少尝试打印一些调试日志,但我看不到我的日志。
解决方案
我无法让它与 Cypress 一起使用,但我可以在 VSCode 中使用 Mocha。
调试步骤:
为您的项目安装 ts-node 和 typescript:
npm i ts-node typescript --save-dev
使用以下内容在您的文件夹中创建 custom-reporter.ts
src
:(取自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--;
}
}
- 我们将在运行时通过 ts-node 编译 custom-reporter.ts,但我们必须将 .js 作为报告器传递给 Mocha。因此,我们按如下方式创建
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"
},
]
}
将 VSCode 中的一些断点放入 src/custom-reporter.ts
在 VSCode 中打开
Run and Debug
面板(Ctrl+Shift+D),选择Debug Mocha tests
并按下播放按钮
这样,您应该能够开始运行测试并在 VSCode 中命中断点。
干杯!
推荐阅读
- reactjs - React Router Redirect没有重定向
- javascript - WebDriverError:未知错误,同时使用 Selenium(Javsacript)打开第二个边缘窗口
- python-2.7 - 拆分字符串以具有单独的引号
- c - __builtin_ia32_unpcklps/hps 和 __builtin_ia32_movlhps/hlps 如何工作?
- ssis - 单击编辑时,ScriptTask 未打开 VSTA IDE。有什么问题?
- ruby-on-rails - 检查请求参数
- c# - 如何在 C# 中从源代码创建哈希码?
- powershell - 如何使 powershell 作业返回我想要的对象类型?
- javascript - Paypal SDK,如何在不重新注入和重新初始化SDK本身的情况下动态更改货币?
- arcgis - 无法为 WebGL 要素图层创建标签,不支持 esriGeometryPolyline