k0p*_*kus 8 javascript node.js chai typescript
我有一个使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果一个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)
Run Code Online (Sandbox Code Playgroud)
我想用更人性化的东西取而代之.有没有办法让chai使用自定义错误信息?
我想能够像这个伪代码一样使用它:
expect(result.length)
.to.be.greaterThan(0)
.withErrorMessage("It should parse at least one sail out of the flatfile, but result is empty");
Run Code Online (Sandbox Code Playgroud)
然后应该打印失败的mocha错误:
AssertionError: It should parse at least one sail out of the flatfile, but result is empty
Run Code Online (Sandbox Code Playgroud)
ale*_*mac 10
每个expect方法都接受一个可选参数message:
expect(1).to.be.above(2, 'nooo why fail??');
expect(1, 'nooo why fail??').to.be.above(2);
Run Code Online (Sandbox Code Playgroud)
所以,在你的情况下它应该是:
expect(result.length)
.to.be.greaterThan(0, "It should parse at least one sail out of the flatfile, but result is empty");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4945 次 |
| 最近记录: |