Any best practices for testing with Jest?
See original GitHub issueI’m using Jest as a test framework. My naive attempts at writing tests for functions that return a Result
typically look like:
expect(callSomeFunction("with", "some", "args")).isOk()).toEqual(true);
expect(callSomeFunction("with", "some", "args"))._unsafeUnwrap()).toEqual(someExpectation);
This is a bit clumsy because:
- I need to double all test calls just for the
isOk()
and_unsafeUnwrap()
parts. Especially if thecallSomeFunction("with", "some", "args")
expression is long, this causes quite some overhead. - When the function fails, the runner only outputs that
isOk()
wasn’ttrue
. Any helpful information in the error object remains hidden. I frequently copy paste the expression into a third line for an ad-hocconsole.log
just to get some information, which of course feels wrong 😉. - Using
_unsafeUnwrap
and_unsafeUnwrapErr
basically everywhere looks a bit ugly and feels like an anti-pattern.
I’m be no means a Jest expert and still have to check if it offers something that could help here.
I just wanted to bring up the topic, perhaps there are already some better approaches?
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
Jest Testing like a Pro - Tips and tricks
The reader should understand your test without reading any other code. Name your tests so well that others can diagnose failures from the...
Read more >Unit Testing Best Practices: 9 to Ensure You Do It Right
Do you know any unit testing best practice? In this post, you'll learn nine of them so you can get started the right...
Read more >Jest · Delightful JavaScript Testing
Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, ......
Read more >Testing with Jest and Enzyme in React — Part 3 (Best ...
Tip 1 — Create a separate file with the global variables · Tip 2 — Use 'describe' and 'it 'keywords appropriately in tests...
Read more >Frontend Unit Testing Best Practices - Meticulous
Frontend Unit Testing Best Practices · 1. Using Linting Rules for Tests · 2. Do Not Repeat Yourself · 3. Group-related Tests in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Cool, thanks for all the fixes.
To summarize the best practices for future readers here: After the changes it is now possible to simply “bring the
ok
/err
to the other side”, i.e.:Alternatively this can be used:
Both approaches should now print very good error messages if a test case returns an
ok
where anerr
is expected or vice versa.Now that this works, I would advice against using a combination of
Not only does it bloat the test code unnecessarily, but it also hides the crucial failure information behind an uninformative
false was not true
failure stemming from theisOk()
check.Here’s a proposed non-breaking change that would allow for generating stack traces in the event that these methods are being used outside of tests and a caller wants stack traces:
https://github.com/supermacro/neverthrow/pull/217/files