Jest has always been my go-to unit testing tool. It is so robust I'm starting to think that I've always been underutilizing it. Although the tests ...
For further actions, you may consider blocking this person and/or reporting abuse
Here's an extra one I love to use:
expect.objectContaining docs.
Really useful for testing objects where you only care about a small part that changes, instead of using snapshots.
+1 for not leaking mocks into global state :)
👏 Bravo for sharing the ability to iterate over test cases. I’ve been looking for something like that for a long time. I’ve hand rolled it myself in jest, Xunit, Nunit, mocha, jasmine, etc. I can’t wait to try this out on Tuesday when I go back to work. Thank you! :)
Great post! Thank you!
I'll also add one little tip for "#3. Snapshot testing on a non-UI elements":
you can replace toMatchSnapshot() by .toMathInlineSnapshot() (check jestjs.io/docs/en/snapshot-testing...) so jest will automatically write all the expected values in your tests file so it's easier to review them (it will even pretty-format them if you use prettier!)
Happy testing!
Majestic looks cool! I like to launch Jest from vs code using ‘—watch’, so it’s running while changing key parts of my components.
Another cool way of using jest is when using the storybook plugin for Structural Testing in React.
I don't know is it correct, my first example:
i dont use any matcher except toEqual because everytime jest upgrades they always mess a few matchers here and there, if i dont use their fancy matchers, my tests have the lowest chance of breaking on every major upgrade
Great tips!!
Did you know about
findRelatedTests
option? I wrote an article about it: dev.to/srshifu/under-the-hood-how-...