Visual Studio Code Mocha ProblemMatcher

Ben*_*oît 5 mocha.js visual-studio-code

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

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

MnZ*_*ZrK 3

似乎所有内置报告器都以可变的行数描述错误。并且不支持为此类报告构建 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);
Run Code Online (Sandbox Code Playgroud)

将命令添加到scripts以下部分package.json

"test": "mocha --reporter my-reporter.js"
Run Code Online (Sandbox Code Playgroud)

然后您添加到您的tasks.json

{
  "type": "npm",
  "script": "test",
  "problemMatcher": [
    {
      "applyTo": "allDocuments",
      "fileLocation": "relative",
      "base": "$tsc-watch",
      "source": "mocha"
    }
  ],
  "group": {
    "kind": "test",
    "isDefault": true
  }
}
Run Code Online (Sandbox Code Playgroud)