question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any best practices for testing with Jest?

See original GitHub issue

I’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 the callSomeFunction("with", "some", "args") expression is long, this causes quite some overhead.
  • When the function fails, the runner only outputs that isOk() wasn’t true. Any helpful information in the error object remains hidden. I frequently copy paste the expression into a third line for an ad-hoc console.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:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
bluenote10commented, Dec 28, 2020

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.:

expect(callSomeFunction("with", "some", "args")).toEqual(ok(someExpectation));

Alternatively this can be used:

expect(callSomeFunction("with", "some", "args")._unsafeUnwrap()).toEqual(someExpectation);

Both approaches should now print very good error messages if a test case returns an ok where an err is expected or vice versa.

Now that this works, I would advice against using a combination of

expect(result.isOk()).toBe(true);
expect(result.value).toHaveProperty("foo");

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 the isOk() check.

1reaction
supermacrocommented, Dec 24, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found