0

My assertion is:

    it.only('should throw an error if the transcription cannot happen', () => {
        expect(TranscriptLib.myFunc({ data }, '1')).to.throw(Error)
    })

My function is:

myFunc: (data, id) => {

                throw new Error({
                    message: 'Transcription failed',
                    error: someError
                })

However, when I run my test, the log shows:

     Error: [object Object]

and the test fails. What am I doing wrong?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 2
    IIRC `expect().to.throw` expects a function, it doesn’t use function-rewriting magic to wrap an expression with a `try`. – Ry- Sep 06 '19 at 22:27
  • @Ry- that did it. Please leave an answer so I can accept – Shamoon Sep 11 '19 at 13:48

1 Answers1

1

Your question is a duplicate of Mocha / Chai expect.to.throw not catching thrown errors

The most straightforward way to fix your issue would be to make the call to your code from an arrow function:

it.only('should throw an error if the transcription cannot happen', () => {   
  expect(() => TranscriptLib.myFunc({ data }, '1')).to.throw(Error)
})

As the answers on the other question show you can also use .bind to create a new function, and so on and so forth.

Louis
  • 146,715
  • 28
  • 274
  • 320