Here is a NodeJS module:
var plural = function(words, num) {
if (words.length != 3) {
throw new Error('Wrong words array');
}
var lastNum = num % 10;
var second = (num / 10) % 10;
if (second == 1)
return words[2]
else if (lastNum == 1)
return words[0];
else if (lastNum <= 4)
return words[1];
else
return words[2];
};
module.exports = plural;
And here is module test:
var expect = require('chai').expect;
var plural = require('../plural')
describe('Test with wrong array', function() {
it('Must throw Error', function() {
expect(plural(['я','меня'])).to.throw(Error);
});
});
I want to test exception throwing. But this is mocha
's output:
Test with wrong array
1) Must throw Error
1 failing
1) Test with wrong array Must throw Error:
Error: Wrong words array
at plural (C:\Users\home\Google Drive\Учеба\Спецкурсы\Яндекс.Интерфейсы\TestableCode\testable-code-01\plural.js:3:9)
at Context.<anonymous> (C:\Users\home\Google Drive\Учеба\Спецкурсы\Яндекс.Интерфейсы\TestableCode\testable-code-01\test\cat.test.js:31:10)
at callFn (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:250:21)
at Test.Runnable.run (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:243:7)
at Runner.runTest (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:373:10)
at C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:451:12
at next (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:298:14)
at C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:308:7
at next (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:246:23)
at Object._onImmediate (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:275:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
So, my code throws exceptions, which is right behavior, and test should be passed. But is doesn't. What's the problem?