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...
For further actions, you may consider blocking this person and/or reporting abuse
var is useful with Jest.
Honestly, I would simply:
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 errormessage
property. IMHO it reads more semantically correct.Cheers,
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
If you actually care about the constructor, you have to pass just it.
I use
a lot nowadays.
Any reason to not put the expect inside the catch bloc ?
Yes. If it doesn't throw assertion wouldn't run, and the test would pass.
You'd have to do
Personally, I'd just go with
(No need for
expect.assertions(1)
since theexpect
runs synchronously inline)Could you please explain why you use only var with jest?
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.
Aha :)
So here is a place where var is still useful 😉
@adrianhelvik
Did you mean:
If i see
var
in your codebase, you're doing it all wrong, even seeinglet
would rise suspicions...Really!
To this extent:)
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.
You made me laugh, Mihail
Did I? Or did I make you exhale audibly through your nose? :D
If I wasn't at work, I would definitely do so :D
So, only laughing is allowed at work.
Got it.
Hmm... animated GIFs work, but not PNGs?
Great Article.
Thanks for sharing this with us, Islam!
My dear friend, thank you.