0

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!');


  })
}
Zepstack
  • 1
  • 2
  • You need to *defer* the error throw, otherwise it happens before `expect` is even called. Pass a function, per the examples: https://www.chaijs.com/api/bdd/#method_throw. – jonrsharpe Mar 12 '21 at 17:18
  • Thanks. My objective is to test that the subtraction function in subtraction.js throws an error if it's called with anything other than numbers. Originally I thought I might test the return value of subtraction with something like ``` expect(subtraction(1, '2')).to.equal( 'subtraction only works with numbers!'); ``` – Zepstack Mar 12 '21 at 18:42
  • Yes, I understand that. But if you call that function and it throws an error you never get to the point of passing anything to expect. Think about a trivial refactor: extract variable. If you did `const result = subtraction(1, '2')` first, would you even *reach* the expectation? – jonrsharpe Mar 12 '21 at 18:42
  • You can't put line breaks in comments. If you need to post more info, [edit] the question. But the solution is illustrated in the docs, or this proposed dupe. – jonrsharpe Mar 12 '21 at 18:47
  • I am new to Mocha, Chai and error testing in this way, and began learning Javascript some weeks ago. So what you say about defering makes me think of using expect(subtraction) rather than expect(subtraction() ), which i had considered, but then I wouldn't have the parameters. Originally I was trying to.equal as in my previous reply above that I accidentally sent too soon. – Zepstack Mar 12 '21 at 18:52
  • I edited my original post to include another attempt using try and catch as demonstrated by the link you provided. But it still isn't accepted as successful. The learning course I am using normally goes step by step even the simplest things, but in the case of this track it seems to just throw you to the wolves-- not sure why that is the case this time. – Zepstack Mar 12 '21 at 19:14
  • Please give a [mre], none of your examples tell us what actually happened. I'd guess in your latest an error object isn't equal to a string, but I don't see why you skipped over the multiple examples of how to pass a function. – jonrsharpe Mar 12 '21 at 19:17
  • In my opening post, I gave the line of code I was having problems with. No other code can be changed but this line. So any solution cannot involve changing other code. That said, if I passed over examples of what I could use in to replace that line of code I cited, then it is just due to my lack of expertise. I can take another look, but I'm not getting it. – Zepstack Mar 12 '21 at 19:35
  • You did, but you just said "not working" and "having problems". Show the output, the error, the delta between that and what you expect. – jonrsharpe Mar 12 '21 at 19:38
  • The only feedback it gives me in regards to my second attempt shown is: "We tried your spec with a version of subtraction.js that works correctly, but the specs didn't pass!" – Zepstack Mar 12 '21 at 19:46
  • Then I'd strongly recommend setting up your local environment so you can run it yourself and see the actual output. Otherwise you've no chance of debugging the problem. Also note a substantial downside to the `try` method is that it can only fail if the function throws but with the wrong error; if it doesn't throw at all no expectation is ever made and the test passes false positive-ly. – jonrsharpe Mar 12 '21 at 20:02
  • jonrsharpe, thank you so much for your prompt attention through all this. It is much appreciated. As it turns out the following code was accepted as correct: expect(subtraction).to.throw(Error); // I've worked through months and months of coursework with this particular site and it hads been great, but this particular course needs to be entirely revamped with a slower more methodical ramp up in what it teaches. Also, the workspaces it provides are disfunctional, but have never been fixed. Thanks, again. I just needed to get past this one hang up. :) – Zepstack Mar 12 '21 at 20:34
  • Note `expect(subtraction).to.throw(Error)` only works because `typeof undefined !== "number"` - it's not a general solution to the testing of things you expect to throw errors. – jonrsharpe Mar 12 '21 at 20:35

0 Answers0