Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,165 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
216,165 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?šŸ™šŸ™