首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调试自定义mocha测试报告器?

如何调试自定义mocha测试报告器?
EN

Stack Overflow用户
提问于 2020-11-10 00:02:48
回答 1查看 182关注 0票数 0

我正在编写一个mocha测试记者,我想用它来编写自定义的Cypress测试文档。哪种方法是调试报表代码的正确方法(也许使用intellij Idea)?

编辑我尝试使用intellij Idea工具进行调试,在调试模式下运行cypress (打开和运行)。我还尝试了允许测试调试的IntelliJ Cypress plugin pro版本。

我不能止步于断点。

所以我至少尝试打印一些调试日志,但是我看不到我的日志。

EN

回答 1

Stack Overflow用户

发布于 2021-06-08 03:54:44

我不能让它在Cypress上工作,但我可以在VSCode中使用Mocha。

Working example here

调试步骤:

为您的项目安装ts-

  • 和typescript:npm i ts-node typescript --save-dev

  1. 在您的src文件夹中使用以下内容创建自定义报告.ts:(摘自https://mochajs.org/api/tutorial-custom-reporter.html,稍作修改)

代码语言:javascript
复制
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--;
  }
}

  1. 我们将在运行时通过ts-node编译自定义报告.ts,但我们必须将.js作为报告器传递给Mocha。因此,我们按如下方式创建index.js,并按如下方式导出我们的报告器:

代码语言:javascript
复制
    module.exports = require("./src/custom-reporter").CustomReporter;

  1. 如果要在不进行调试的情况下运行,请将测试脚本添加到package.json中:

代码语言:javascript
复制
 "scripts": {
    "test": "mocha -r ts-node/register specs/*.spec.ts --reporter index"
  },

  1. 在项目根目录中创建.vscode/launch.json,并添加以下代码:

代码语言:javascript
复制
{
    // 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中的

  1. 在VSCode中放置一些断点

  1. In VSCode打开Run and Debug面板(Ctrl+Shift+D),选择Debug Mocha tests并按play按钮

这样,您应该能够启动测试运行并命中VSCode中的断点。

干杯!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64755044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档