I'm using https://www.chaijs.com/api/bdd/ as a resource. My objective is to test that the subtraction function in subtraction.js throws an error if it's called with anything other than numbers.
I also have to check for subtraction's specific error message.
I'm having problems with this line of code :
expect(subtraction(1, '2')).to.throw(TypeError, 'subtraction only works with numbers!');
from the context shown below:
var expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
expect(subtraction(1, '2')).to.throw(TypeError, 'subtraction only works with numbers!');
})
})
given substraction.js is:
function subtraction (number1, number2) {
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw Error('subtraction only works with numbers!')
}
return number1 - number2
}
///////////
ANOTHER ATTEMPT:
var expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
try {
subtraction(1,'2');
}
catch(err) {
expect(err).to.equal('subtraction only works with numbers!');
})
}