Chai, Mocha: Identify follows approval

I use mocha and chai as statements.

I have several statements in my specification

Exp1.should.be.true Exp2.should.be.true Exp3.should.be.true

If one of them does not work, mocha writes that the "expected false value is true", is there a way to identify them?

With the wait, I can do this: wait (Exp1, 'Exp1'). to.be true

Is it possible that this is possible?

+4
source share
3 answers

Apparently, should not support custom error messages at the moment.

You can create your own helper to install the message:

 var chai = require('chai'), should = chai.should(); // Helper definition - should be in a shared file chai.use(function(_chai, utils) { _chai.Assertion.addMethod('withMessage', function(msg) { utils.flag(this, 'message', msg); }); }); // Sample usage it('should fail', function() { var Exp1 = false; var Exp2 = false; Exp1.should.be.withMessage('Exp1').true; Exp1.should.withMessage('Exp2').be.true; }); 
+5
source

I checked the chai code against and found that the currently accepted answer is either incorrect or incomplete.

If you read there, you will find that in each statement there really is a way to include your own message. The catch is that you may need to change the statement syntax to use should function calls instead.

 (1).should.equal(0, 'This should fail'); /****** Output with (I believe) default reporter ***** * This should fail * + expected - actual * * -1 * +0 */ 

Please note that your result may look different if you use your own reporter. If you feel so inclined, perhaps you can include should functions to always include the line number in your statement output.

+2
source

I wonder why they just don’t add which line dismissed this statement, but I ran into this same problem. A colleague who can help better than I noticed, there is a setting for includeStack that will give line numbers for statements. http://chaijs.com/guide/styles/#configure

Since I do a lot of asynchronization, I can run my tests in before or beforeEach and then run a separate it for each statement.

0
source

All Articles