我正在 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/