When running a test in a loop with Mocha and Chai, it might not be easy to pinpoint the element where the error occurred. For example:
1 2 3
| for (let i = 0, len = arr.length; i < len; i += 1) { expect(arr[i]).to.equal(true); }
|
Run the test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| $ mocha --harmony test/message.js Custom Message 1) Should return true 0 passing (37ms) 1 failing 1) Custom Message Should return true: AssertionError: expected false to equal true + expected - actual -false +true at Context.<anonymous> (test/message.js:16:25)
|
It failed on the expect
statement, but the error message does not show which element is in error.
Instead, we can add a custom message to the expect
statement:
1 2 3
| for (let i = 0, len = arr.length; i < len; i += 1) { expect(arr[i], 'i = ' + i).to.equal(true); }
|