【问题标题】:Visual Studio Code Mocha ProblemMatcherVisual Studio Code Mocha ProblemMatcher
【发布时间】:2017-07-21 09:07:35
【问题描述】:

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

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

【问题讨论】:

    标签: mocha.js visual-studio-code


    【解决方案1】:

    似乎所有内置报告器都使用可变行数来描述错误。并且不支持为此类报告构建 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'。有什么想法吗?
    • 我想我通过包含更新版本的 Mocha 解决了上述问题。
    猜你喜欢
    • 2017-02-02
    • 2017-08-17
    • 2015-07-13
    • 1970-01-01
    • 2017-02-15
    • 2020-12-22
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多