繁体   English   中英

如何用 jest 测试 TaskEither form fp-ts

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

我是 fp-ts 的新手。 假设我有一个函数(path: string) => TaskEither<Erorr, T>读取和解析配置,我想为此编写一个测试。

到目前为止,我有:

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()
      })
    )
  )

})

但是由于超时而失败。 而且我不确定我是否正确实现了折叠。 通常如何将TaskEither折叠到Task然后异步调用它?

我错过了函数调用。

它应该是:

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

这篇文章是我在寻找如何用 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