Visual Studio Code Mocha问题匹配器

5
1个回答

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

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
  }
}

我收到了 Error: Cannot find module 'stacktrace-parser' 的错误信息。有什么想法吗? - Zach
我认为我已经修复了上面的问题,包括了Mocha的更新版本。 - Zach

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接