Visual Studio Code Mocha ProblemMatcher

我正在Visual Studio Code中搜索Mocha问题匹配器的配置.问题匹配器检查终端输出是否有错误,并将它们添加到“问题”视图中.

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

最佳答案 好像所有内置记者都用可变行数来描述错误.并且不支持为此类报告构建vscode问题匹配器 – 多行支持非常有限.

但我们可以用我们喜欢的任何格式建立自己的记者,然后轻松匹配!

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

// 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);

将命令添加到package.json中的scripts部分:

"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
  }
}
点赞