简体   繁体   中英

How to test TaskEither form fp-ts with jest

I am new to fp-ts. Let's say I have a function (path: string) => TaskEither<Erorr, T> which reads and parses config, and I want to write a test for that.

So far I have:

test('Read config', done => {
  interface Config {
    fld1: string
    fld2: {
      fld: 3
    }
  }

  pipe(
    readConfig<Config>("resources/test-config.toml"),
    TE.fold(
      err => T.of(done(err)),
      toml => T.of(() => {
        expect(toml).toBe({})
        done()
      })
    )
  )

})

But it fails due to timeout. And also I am unsure if I implemented fold correctly. How do fold TaskEither to Task in general and then call it asynchronously?

I was missing function call.

It should have been:

  pipe(
    readConfig<Config>("resources/test-config.toml"),
    TE.fold(
      err => T.of(done(err)),
      toml => T.of(() => {
        expect(toml).toBe({})
        done()
      })
    )
  )()

This post was the only I found when I was looking how to test TaskEither with jest, so here is my answer:

import * as TE from "fp-ts/lib/TaskEither";
import * as E from "fp-ts/lib/Either";

it("tests TaskEither", async () => {
  const fn = TE.right("something");
  await expect(fn()).resolves.toStrictEqual(E.right("something"));
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM