Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Improving Our Tests Catching an Error

Aaron Bell
Aaron Bell
6,113 Points

How does this spec not check for subtraction's desired behavior?

I feel like this should check subtraction throws an error. I would prefer a more useful error message so that I know where I'm messing up (or if I'm actually messing up).

It's for this coding challenge here: https://teamtreehouse.com/library/javascript-unit-testing/improving-our-tests/catching-an-error.

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('a', 1)).to.throw();
    expect(subtraction(1, 'a')).to.throw();
    expect(subtraction('b', 'a')).to.throw();
    expect(subtraction([1.3], 'a')).to.throw();
    expect(subtraction(2, 1)).to.not.throw();
  })
})
subtraction.js
function subtraction (number1, number2) {
  if (typeof number1 !== 'number' || typeof number2 !== 'number') {
    throw Error('subtraction only works with numbers!')
  }
  return number1 - number2
}

5 Answers

Steven Parker
Steven Parker
231,261 Points

The argument to "expect" needs to be a function that will cause "subtraction" to be called. If you call it yourself in the argument, it will be done (exception or not) and the argument passed to "expect" will be the result.

Eden Gomez
Eden Gomez
22,565 Points

var expect = require('chai').expect

describe('subtraction', function () { var subtraction = require('../WHEREVER')
it('only works with numbers', function () { // YOUR CODE HERE //var = expect(substraction(number1, number2).to.be.NaN); }) })

Eden Gomez
Eden Gomez
22,565 Points
var expect = require('chai').expect

describe('subtraction', function () { var subtraction = require('../WHEREVER')
it('only works with numbers', function () { 
// YOUR CODE HERE 
 expect(substraction(number1, number2).to.be.NaN); }) 
})

Im sorry but im lost, can somebody give us a hint??? i dont know what method should i use

Steven Parker
Steven Parker
231,261 Points

To get the best chance of a community response, always start a fresh question instead of asking one as an "answer" to another question.

Eden Gomez
Eden Gomez
22,565 Points

Already did it Steven Parker Iā€™m still stuck in that challenge. Do you mind give me an extra hint?šŸ™šŸ™