gpt4 book ai didi

typescript - fp-ts 和 Jest : ergonomic tests for Option and Either?

转载 作者:行者123 更新时间:2023-12-02 03:03:13 31 4
gpt4 key购买 nike

我正在使用 fp-ts,并使用 Jest 编写单元测试。在许多情况下,我正在测试可为 null 的结果,通常用 OptionEither 表示(通常是数组 find)。如果结果为none(以Option为例),并且继续知道这个结果是some,最符合人体工程学的方法是什么?

以下是我目前如何解决问题的示例:

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

但是必须编写一个提前返回的 if 不太符合人体工程学。

我也可以编写一个 as 断言:

  // ...
it(`should do something with a "some" result`, () => {
const result = someFunc('abcd') as Option.Some<string>;

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

但缺点是我必须重写 some 的类型。在很多情况下,编写它很繁重,需要编写并导出接口(interface)以进行测试(这也不符合人体工程学)。

有什么办法可以简化这种测试吗?

编辑:这是一个更接近实际情况的测试用例:


interface SomeComplexType {
id: string,
inputAsArray: string[],
input: string;
}

function someFunc(input: string): Option.Option<SomeComplexType> {
return Option.some({
id: '5',
inputAsArray: input.split(''),
input,
});
}

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

// This is the un-ergonomic step
if(Option.isNone(result)) {
expect(Option.isSome(result)).toEqual(true);
return;
}

// Ideally, I would only need this:
expect(Option.isSome(result)).toEqual(true);
// Since nothing will be ran after it if the result is not "some"
// But I can imagine it's unlikely that TS could figure that out from Jest expects

// Since I now have the value's actual type, I can do a lot with it
// I don't have to check it for nullability, and I don't have to write its type
const myValue = result.value;

expect(myValue.inputAsArray).toEqual(expect.arrayContaining(['a', 'b', 'c', 'd']));

const someOtherThing = getTheOtherThing(myValue.id);

expect(someOtherThing).toMatchObject({
another: 'thing',
});
});
});

最佳答案

您可以编写不安全的转换fromSome,如下所示:

function fromSome<T>(input: Option.Option<T>): T {
if (Option.isNone(input)) {
throw new Error();
}
return input.value;
}

然后在测试中使用它

  it(`should do something with a "some" result`, () => {
const result = someFunc('abcd');
const myValue = fromSome(result);
// do something with myValue
});

关于typescript - fp-ts 和 Jest : ergonomic tests for Option and Either?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59630012/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com