DEV Community

Discussion on: 1. let, const and ... var

Collapse
 
youbicode profile image
A.

Any reason to not put the expect inside the catch bloc ?

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Yes. If it doesn't throw assertion wouldn't run, and the test would pass.
You'd have to do

expect.assertions(1)
try {
  await foo()
} catch (e) {
  var error = e
  expect(error.message).toMatch(/foo/)
}

Personally, I'd just go with

await expect(foo()).rejects.toThrow('foo')
// Or, if final in the test:
return expect(foo()).rejects.toThrow('foo') // doesn't require `async` `it`

(No need for expect.assertions(1) since the expect runs synchronously inline)