javascript - How to provide chai expect with custom error message for mocha unit test? -


i have mocha test using chai's expect:

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 1 to.be.greaterthan(0) expectation fails, error output on mocha not dev-friendly:

 assertionerror: expected 0 above 0       @ assertion.assertabove (node_modules/chai/lib/chai/core/assertions.js:571:12)       @ assertion.ctx.(anonymous function) [as greaterthan] (node_modules/chai/lib/chai/utils/addmethod.js:41:25)       @ _callee2$ (tests/unit/operator/croisieurope/croisxmlextractortest.js:409:61)       @ trycatch (node_modules/regenerator-runtime/runtime.js:65:40)       @ generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)       @ generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)       @ fulfilled (node_modules/tslib/tslib.js:93:62)       @ <anonymous>       @ process._tickdomaincallback (internal/process/next_tick.js:228:7) 

i replace more human-friendly. there way tell chai use customize error message?

i want able use pseudo-code:

 expect(result.length)       .to.be.greaterthan(0)       .witherrormessage("it should parse @ least 1 sail out of flatfile, result empty"); 

and failing mocha error should print:

 assertionerror: should parse @ least 1 sail out of flatfile, result empty 

every expect method accepts optional parameter message:

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

so, in case should be:

expect(result.length)   .to.be.greaterthan(0, "it should parse @ least 1 sail out of flatfile, result empty"); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -