如何防止我的结果在 Mocha Junit 中被覆盖

How can I keep my results from overwriting themselves in Mocha Junit

我正在使用 Cypress 和 Mocha Junit 在 Chrome 中对 React 进行端到端测试。默认行为是每次运行我的测试时它只输出一个结果,并且每次都会覆盖文件。我想让它像日志一样保存这些文件。

配置位于 JSON 文件中,如下所示:

}
  "projectId": "XXXXXX",
  "reporter": "junit",
  "reporterOptions": {
    "mochaFile": "./cypress/results/my-test-output.xml",
    "toConsole": true
  }
}

我想做类似

的事情
var date = new Date();
"mochaFile": "./cypress/results/my-test-output${date}.xml",

显然这是无效的 JSON。我怎样才能让它每次都生成独特的东西?

来自文档,

Results XML filename can contain [hash]

此外,如果您查看 junit 报告程序 source,您可以了解它是如何工作的:

...
this.writeXmlToDisk(xml, this._options.mochaFile);
...

MochaJUnitReporter.prototype.writeXmlToDisk = function(xml, filePath){
  if (filePath) {
    if (filePath.indexOf('[hash]') !== -1) {
      filePath = filePath.replace('[hash]', md5(xml));
    } ...

所以,您可以:

"mochaFile": "./cypress/results/my-test-output[hash].xml"

这里无事可做。如您所见,您需要做的就是将 JSON 包裹在反引号中。除了我建议您使用 Date.now() 而不是 new Date()

let date = Date.now();
let json = `{
  "projectId": "XXXXXX",
  "reporter": "junit",
  "reporterOptions": {
    "mochaFile": "./cypress/results/my-test-output-${date}.xml",
    "toConsole": true
  }
}`

let parsedJson = JSON.parse(json)
console.log(parsedJson.reporterOptions.mochaFile)

与哈希相比,这甚至具有历史化的优势。