mardi 7 janvier 2020

fp-ts and jest: ergonomic tests for Option and Either?

I'm using fp-ts, and I write unit tests with Jest. In many cases, I'm testing nullable results, often represented with Option or Either (typically, array finds). What is the most ergonomic way to make the test fail if the result is none (taking Option as an example), and keep on going knowing that this result is some?

Here's an example of how I can solve the problem at the moment:

function someFunc(input: string): Option.Option<string> {
  return Option.some(input);
}

describe(`Some suite`, () => {
  it(`should do something with a "some" result`, () => {
    const result = someFunc('abcd');

    // This is a fail case, here I'm expecting result to be Some
    if(Option.isNone(result)) {
      expect(Option.isSome(result)).toEqual(true);
      return;
    }

    expect(result.value).toEqual('abcd');
  });
});

But having to return early and add a a check is not very ergonomic. Is there any way to simplify this kind of test?

Aucun commentaire:

Enregistrer un commentaire