DEV Community

Cover image for 1. let, const and ... var

1. let, const and ... var

Islam Sayed on February 11, 2019

ES6 has introduced some new syntax features. One of them was key words const, and let to declare variables. Let's talk about why they are preferred...
Collapse
 
adrianhelvik profile image
Adrian

var is useful with Jest.

try {
  await foo()
} catch (e) {
  var error = e
}
expect(error.message).toMatch(/foo/)
Collapse
 
cms profile image
Christian C. Salvadó

Honestly, I would simply:

await expect(foo()).rejects.toThrow(/foo/)

Since foo is an async function, it implicitly returns a promise so you can use the .resolves / .rejects matchers and then use the toThrow method, and this accepts a regex or string to match the error message property. IMHO it reads more semantically correct.

Cheers,

Collapse
 
qm3ster profile image
Mihail Malo

The toThrow API is quite weird.
If you pass a string, it matches it anywhere, so 'foo' and /foo/ is the same.
And if you want to strictly match the whole message, you need to do

.toThrow(/^literal message$/) // RegEx with ends
.toThrow(new Error('literal message')) // The Error class does NOT matter in this case.

If you actually care about the constructor, you have to pass just it.

.toThrow(SpecialError)

I use

expect(badFunc).toThrowErrorMatchingInlineSnapshot()

a lot nowadays.

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)

Collapse
 
islam profile image
Islam Sayed

Could you please explain why you use only var with jest?

Collapse
 
adrianhelvik profile image
Adrian

Oh, no. I only ever use var in this scenario. Lets me skip out of the block scope without a separate variable declaration. And if no error happens, the variable is just undefined.

Thread Thread
 
islam profile image
Islam Sayed

Aha :)
So here is a place where var is still useful 😉

Thread Thread
 
qm3ster profile image
Mihail Malo • Edited

@adrianhelvik
Did you mean:

let error
try {
  await foo()
} catch (e) {
  error = e
}
expect(error.message).toMatch('foo') // you can use string literals if you don't have a complex RegEx
Collapse
 
avatsaev profile image
Aslan Vatsaev

If i see var in your codebase, you're doing it all wrong, even seeing let would rise suspicions...

Collapse
 
islam profile image
Islam Sayed

Really!
To this extent:)

Collapse
 
qm3ster profile image
Mihail Malo • Edited

You know what would be RADICAL?
Renaming this article to const, let, and... var, to comply with the the good, the bad, and the ugly formula.

Collapse
 
islam profile image
Islam Sayed • Edited

You made me laugh, Mihail

Collapse
 
qm3ster profile image
Mihail Malo

Did I? Or did I make you exhale audibly through your nose? :D

Thread Thread
 
islam profile image
Islam Sayed

If I wasn't at work, I would definitely do so :D

Thread Thread
 
qm3ster profile image
Mihail Malo • Edited

So, only laughing is allowed at work.
Got it.
salute

Hmm... animated GIFs work, but not PNGs?

Collapse
 
elharony profile image
Yahya Elharony

Great Article.
Thanks for sharing this with us, Islam!

Collapse
 
islam profile image
Islam Sayed

My dear friend, thank you.