JavaScript JavaScript Unit Testing Improving Our Tests Catching an Error

Roland Abregorivas
Roland Abregorivas
16,784 Points

Dont understand the question regarding subtraction Spec. Can someone explain what they are looking for.

Not sure what to test,

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

describe('subtraction', function () {
  var subtraction = require('../WHEREVER')  
  it('only works with numbers', function () {
    // YOUR CODE HERE
    //expect(subtraction(2,1)).to.equal(1)
    expect(subtraction("1","2").to.be.a('string', 'subtraction only works with numbers!')
  })
})
c
subtraction.js
function subtraction (number1, number2) {
  if (typeof number1 !== 'number' || typeof number2 !== 'number') {
    throw Error('subtraction only works with numbers!')
  }
  return number1 - number2
}
Roland Abregorivas
Roland Abregorivas
16,784 Points

Here is the question. The subtraction function in subtraction.js throws an error if it's called with anything other than numbers. Write a spec that will test for this behavior. Make sure to check for subtraction's specific error message! Check the Chai docs if you need help.