0

I'm trying to write a test case that catches an error "Error: Please provide pitchWidth and pitchHeight". But I cannot seem to get the expect to catch the throw as a successful test.

Code:

mocha.describe('testValidationOfBadInputData()', function() {
mocha.it('init game fails on pitch height', async() => {
  let t1location = './init_config/team1.json'
  let t2location = './init_config/team2.json'
  let plocation = './test/input/badInput/badPitchHeight.json'
  // let badFn = await validation.initGame(t1location, t2location, plocation)

  expect(await validation.initGame(t1location, t2location, plocation)).to.throw()
}) })

Output:

1) testValidationOfBadInputData()
       init game fails on pitch height:
     Error: Please provide pitchWidth and pitchHeight
      at Object.validatePitch (lib/validate.js:56:11)
      at Object.initiateGame (engine.js:18:12)
      at Object.initGame (test/lib/validate_tests.js:9:29)       

Other Attempts have also failed:

1)

expect(await validation.initGame(t1location, t2location, plocation)).to.throw(Error, 'Please provide pitchWidth and pitchHeight');

2)

expect(await validation.initGame.bind(t1location, t2location, plocation)).to.throw();

Not sure what I'm doing wrong and the documentation doesn't seem obvious. https://www.chaijs.com/api/bdd/#method_throw

async function initGame(t1, t2, p) {       
  let team1 = await common.readFile(t1)       
  let team2 = await common.readFile(t2)       
  let pitch = await common.readFile(p)       
  let matchSetup = engine.initiateGame(team1, team2, pitch)     
 return matchSetup  
}

the above is the function I am calling.

2 Answers2

0

I think this look similar to a problem I had yesterday and matches to this question: Is node's assert.throws completely broken?

The function is executing in the stack before being passed to expect().

Instead try

expect(function() { await validation.initGame(t1location, t2location, plocation); }).to.throw()
MattH
  • 4,166
  • 2
  • 29
  • 33
0

I was able to create correct tests by doing the following:

 mocha.describe('testValidationOfBadInputData()', function() {
    mocha.it('init game fails on pitch height', async() => {
      let t1location = './init_config/team1.json'
      let t2location = './init_config/team2.json'
      let plocation = './test/input/badInput/badPitchHeight.json'
      try{
        await validation.initGame(t1location, t2location, plocation); 
      }catch(err){
        expect(err).to.be.an('Error');
        expect(err.toString()).to.have.string('Error: Please provide pitchWidth and pitchHeight')
      }
    })
  })

As Matt described, I tried to call the function within a function on the expect. This wanted an asynchronous function (because I'm using await) but then failed with

UnhandledPromiseRejectionWarning: Error: Please provide pitchWidth and pitchHeight

Which made me think of putting it into a try catch block and then handling the error we get back.

In the catch block;

  1. Expect the output to be an error

    expect(err).to.be.an('Error');

  2. Match the string version of the error to the expected output

    expect(err.toString()).to.have.string('Error: my error')

This might not be the best solution. Happy to take other answers.