简体   繁体   English

如何为chai Expect提供用于摩卡单元测试的自定义错误消息?

[英]How to provide chai expect with custom error message for mocha unit test?

I have a mocha test using chai's expect: 我使用chai的期望进行了摩卡测试:

it("should parse sails out of cache file", async () => {
    const sailExtractor = new Extractor();
    const result = await sailExtractor.extract("test.xml");

    try {
        expect(result.length).to.be.greaterThan(0);
        const withMandatoryFlight = result.filter((cruises) => {
            return cruises.hasMandatoryFlight === true;
        });
        expect(withMandatoryFlight.length).to.be.greaterThan(0);
        const cruiseOnly = result.filter((cruises) => {
            return cruises.hasMandatoryFlight === false;
        });

        expect(cruiseOnly.length).to.be.greaterThan(0);

        return Promise.resolve();
    } catch (e) {
        return Promise.reject(e);
    }
}

Now if one to.be.greaterThan(0) expectation fails, the error output on mocha is not dev-friendly: 现在,如果一个to.be.greaterThan(0)期望失败,则在mocha上输出的错误将不适合开发人员:

 AssertionError: expected 0 to be above 0
      at Assertion.assertAbove (node_modules/chai/lib/chai/core/assertions.js:571:12)
      at Assertion.ctx.(anonymous function) [as greaterThan] (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
      at _callee2$ (tests/unit/operator/croisiEurope/CroisXmlExtractorTest.js:409:61)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
      at fulfilled (node_modules/tslib/tslib.js:93:62)
      at <anonymous>
      at process._tickDomainCallback (internal/process/next_tick.js:228:7)

I would like to replace it with something more human-friendly. 我想用更人性化的东西代替它。 Is there a way to tell chai to use a customize error message? 有没有办法告诉chai使用自定义错误消息?

I want to be able to use it like this pseudo-code: 我希望能够像下面的伪代码一样使用它:

 expect(result.length)
      .to.be.greaterThan(0)
      .withErrorMessage("It should parse at least one sail out of the flatfile, but result is empty");

And then the failing mocha error should print: 然后应该显示失败的摩卡错误:

 AssertionError: It should parse at least one sail out of the flatfile, but result is empty

Every expect method accepts an optional parameter message : 每个expect方法都接受一个可选的参数message

expect(1).to.be.above(2, 'nooo why fail??');
expect(1, 'nooo why fail??').to.be.above(2);

So, in your case it should be: 因此,在您的情况下,应为:

expect(result.length)
  .to.be.greaterThan(0, "It should parse at least one sail out of the flatfile, but result is empty");

If you use should for your assertions you can pass a string to the test function which will be written out if the condition fails. 如果您使用should为您的断言,则可以将字符串传递给测试函数,如果条件失败,该函数将被写出。 For example: 例如:

result.length.should.be.above(0, "It should parse at least one sail out of the flatfile, but result is empty");

I'm not sure if this is possible with expect. 我不确定这是否可以实现。 The API doesn't seem to mention it. 该API似乎没有提及它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何期望与Mocha和Chai投球? - How to expect throw with Mocha and Chai? 如何使用酶,摩卡,柴对单元自定义的React函数进行单元测试 - How do you unit test a custom React function using enzyme, mocha, chai 如何对路由器功能NodeJS / Mocha / Chai进行单元测试 - How to carry unit test on router function NodeJS / Mocha / Chai 如何使用摩卡茶和酶对道具的状态进行单元测试? - How to unit test the state of props using mocha chai and enzyme? Chai期望抛出错误信息 - Chai expect to throw with error message 如何使用Mocha + Chai编写测试以期望setTimeout有异常? - How to write a test using Mocha+Chai to expect an exception from setTimeout? Mocha / Chai:测试精确的投掷错误结果 - Mocha/Chai: Test for exact throw error result 如何编写用于使用mongoose,mocha和chai将对象插入和检索到mongodb的单元测试? - How to write a Unit test for inserting and retrieving an object to mongodb using mongoose, mocha and chai? 如何使用业力摩卡/柴酶对基本三元进行单元测试? - how to unit test a basic ternary using karma-mocha/chai-enzyme? 如果变量具有反应组件作为值,我如何使用 mocha/chai/sinon 进行单元测试? - How do i unit test using mocha/chai/sinon if a variable has a react component as value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM