如何为摩卡unit testing提供自定义错误消息的chai expect?
我有一个使用柴的期望的摩卡testing:
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); } }
现在,如果一个to.be.greaterThan(0)
期望失败,摩卡上的错误输出不友好:
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)
我想用更人性化的东西来代替它。 有没有办法告诉柴使用自定义错误消息?
我想能够像这样的伪代码使用它:
expect(result.length) .to.be.greaterThan(0) .withErrorMessage("It should parse at least one sail out of the flatfile, but result is empty");
然后失败的摩卡错误应该打印:
AssertionError: It should parse at least one sail out of the flatfile, but result is empty
每个expect
方法接受一个可选的参数message
:
expect(1).to.be.above(2, 'nooo why fail??'); expect(1, 'nooo why fail??').to.be.above(2);
所以,你的情况应该是:
expect(result.length) .to.be.greaterThan(0, "It should parse at least one sail out of the flatfile, but result is empty");
如果你用你的断言,你可以传递一个string到testing函数,如果条件失败将被写出来。 例如:
result.length.should.be.above(0, "It should parse at least one sail out of the flatfile, but result is empty");
我不确定这是否可能与预期。 API似乎没有提到它。
- Visual Studio 2013可以在NodeJs Web应用程序中debugging客户端JavaScript
- Visual Studio不能将JavaScript输出组合成一个文件。 我需要做更多的调整吗?
- 如何从打字稿中使用npm模块?
- TypeScript或CoffeeScript
- NodeJS,TypeScript和typescript集合
- TypeScript编译器很慢(6-10s)
- visual studio没有findfirebase节点模块
- stripe angular错误:Uncaught(在promise中):TypeError:无法读取未定义的属性'stripeService'
- mongoose的打字方式…?