mocha.js - Visual Studio Code Mocha ProblemMatcher

标签 mocha.js visual-studio-code

我正在 Visual Studio Code 中搜索 Mocha 问题匹配器的配置。问题匹配器检查终端输出中的错误并将它们添加到问题 View 中。

VS Code 中的问题匹配器描述如下:https://code.visualstudio.com/docs/editor/tasks#_processing-task-output-with-problem-matchers

最佳答案

似乎所有内置报告器都用可变行数来描述错误。并且不支持为此类报告构建 vscode 问题匹配器 - 多行支持极其有限。

但是我们可以用我们喜欢的任何格式构建我们自己的报告器,然后轻松地匹配它!

这是一个简单的报告器,它扩展了默认的 Spec 报告器,输出错误 $tsc-watch匹配器兼容格式:

// my-reporter.js
var StackTraceParser = require('stacktrace-parser');
var path = require('path');
var mocha = require('mocha');
module.exports = MyReporter;

function MyReporter(runner) {
  mocha.reporters.Spec.call(this, runner);

  runner.on('fail', function(test, err){
    var lines = StackTraceParser.parse(err.stack)
    // we are only interested in the place in the test which originated the error
    var line = lines.find(line => line.file.startsWith('test'))
    if (line) {
      console.log(`${line.file}(${line.lineNumber},${line.column}): error TS0000: ${err.message.split('\n')[0]}`)
    }
  });

}

// To have this reporter "extend" a built-in reporter uncomment the     following line:
mocha.utils.inherits(MyReporter, mocha.reporters.Spec);

将命令添加到 scripts package.json 中的部分:
"test": "mocha --reporter my-reporter.js"

然后添加到您的 tasks.json :
{
  "type": "npm",
  "script": "test",
  "problemMatcher": [
    {
      "applyTo": "allDocuments",
      "fileLocation": "relative",
      "base": "$tsc-watch",
      "source": "mocha"
    }
  ],
  "group": {
    "kind": "test",
    "isDefault": true
  }
}

关于mocha.js - Visual Studio Code Mocha ProblemMatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45233591/

相关文章:

javascript - 为什么sinon认不出我的 stub ?

javascript - Mocha JS : How to reference members from asynch before function in describe/it test functions

javascript - 如何在 NodeJS 中测试递归调用的函数?

node.js - 为什么 mocha 不显示通过的测试?像开玩笑。它只是说 5 测试通过没有更多细节

javascript - 如何在 VS code/Prettier 中为不同文件指定不同格式

node.js - 使用 mongoose createConnection 超时以使用 mocha 进行测试

Python 需要安装 ipykernel

git - 在 VSCode 中克隆 GitHub 存储库

typescript - 向空对象添加属性

python - Visual Studio 代码交互式 Python 控制台