如何使用Chai在mocha框架中使用backstopjs断言失败

问题描述 投票:0回答:1

我正在尝试使用mocha创建一个视觉回归框架,使用backstopjs完成视觉差异。

我有一个小的第一次测试运行,但是当我故意未通过测试时,我希望mocha框架能够确认失败并报告此情况。事实上,当运行mocha框架时,一切都按照我的预期进行,但是即使backstopjs报告失败,mocha报告仍然显示为通过。

这是我的样本测试

const assert = require('chai').assert
const backstop = require('backstopjs')


describe('Navigation To Google', () => {
  it('and take a screenshot', () => {

    backstop('test', {config: './backstop.json'})
     .then(() => {
        console.log('backstop test')
     }).catch((e) => {
         //If fails catch error here
         console.log(`The error is ${e}`)
     })

     // I assume I need to handle the assertion here

  });
})

我的github和代码可以在这里查看:https://github.com/Kpizzle/PupperDif

javascript mocha chai backstop.js
1个回答
0
投票

解决我的问题,我对Javascript很新,但仍然要围绕它的异步性质。

我在代码中添加了异步等待。查看新代码。

const assert = require("chai").assert;
const backstop = require("backstopjs");

describe("Navigation To Google", () => {
it("and take a screenshot", async () => {
await backstop("test", { config: "./backstopScenarios/backstop.google.json" })
  .then(() => {
    console.log("backstop test");
  })
  .catch(e => {
    //If fails catch error here
    assert.ifError(e);
  });
 });
});
© www.soinside.com 2019 - 2024. All rights reserved.