JavaScript JavaScript Unit Testing Improving Our Tests Catching an Error

Vitaly Kashubin
Vitaly Kashubin
8,449 Points

Can't find a fault! Could you please help me?

I don`t understand where am i wrong?

subtraction_spec.js
var expect = require('chai').expect

describe('subtraction', function () {
  var subtraction = require('../WHEREVER')  
  it('only works with numbers', function () {
    let handel = function (){subtraction("la",12);};
    expect(handel).to.throw(Error);
    expect(handel).to.throw('subtraction only works with numbers!');
  })
})
subtraction.js
function subtraction (number1, number2) {
  if (typeof number1 !== 'number' || typeof number2 !== 'number') {
    throw Error('subtraction only works with numbers!')
  }
  return number1 - number2
}

1 Answer

Not sure why but I changed let to var and it passed