简体   繁体   English

如何用 jest 测试 TaskEither form fp-ts

[英]How to test TaskEither form fp-ts with jest

I am new to fp-ts.我是 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.假设我有一个函数(path: string) => TaskEither<Erorr, T>读取和解析配置,我想为此编写一个测试。

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?通常如何将TaskEither折叠到Task然后异步调用它?

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:这篇文章是我在寻找如何用 jest 测试 TaskEither 时发现的唯一一篇文章,所以这是我的答案:

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"));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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