Skip to content

Any best practices for testing with Jest? #211

@bluenote10

Description

@bluenote10

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions