#prefer-result-matchers
テストで @praha/byethrow-testing の toBeSuccess() / toBeFailure() マッチャーを使うことを強制します。
このルールは自動修正に対応しています。推奨プリセットでは、テストファイルに対してのみ有効になります。
#ルールの詳細
Result.isSuccess() が返す真偽値に対してアサーションを書くのは冗長で、失敗しても false が true ではなかったことしかわかりません。
また、テスト内で Result.unwrap() を呼ぶと、結果が失敗だった場合に役に立たないエラーが投げられます。
専用のマッチャーを使えば記述が短くなり、失敗時には受け取った Result が表示され、コールバックでは型の付いた値を受け取れます。
このルールは次のパターンを報告します。
expect(Result.isSuccess(result))/expect(Result.isFailure(result))に続く.toBe()、.toBeTruthy()、.toBeFalsy()assert(Result.isSuccess(result))/assert(Result.isFailure(result))Result.unwrap(result)/Result.unwrapError(result)
#誤った例
import { import Result Result } from '@praha/byethrow';
import { const assert: Chai.Assert assert , const expect: ExpectStatic expect } from 'vitest';
declare const const result: Result.Result<string, Error> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <string, Error>;
// ❌ isSuccess / isFailure が返す真偽値に対してアサーションしている
expect<boolean>(actual: boolean, message?: string): Assertion<void, boolean> (+1 overload) expect (import Result Result .function isSuccess<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Success<string>Type guard to check if a
Result
is a
Success
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
console.log(result.value); // Safe access to value
}
@categoryType Guards isSuccess (const result: Result.Result<string, Error> result )).JestAssertion<void, boolean>.toBe: <boolean>(expected: boolean) => voidChecks that a value is what you expect. It calls Object.is to compare values.
Don't use toBe with floating-point numbers.
@exampleexpect (result).toBe(42);
expect(status).toBe(true); toBe (true);
expect<boolean>(actual: boolean, message?: string): Assertion<void, boolean> (+1 overload) expect (import Result Result .function isFailure<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Failure<Error>Type guard to check if a
Result
is a
Failure
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Failure , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
if (Result.isFailure(result)) {
console.error(result.error); // Safe access to error
}
@categoryType Guards isFailure (const result: Result.Result<string, Error> result )).JestAssertion<void, boolean>.toBeTruthy: () => voidUse when you don't care what a value is, you just want to ensure a value
is true in a boolean context. In JavaScript, there are six falsy values:
false, 0, '', null, undefined, and NaN. Everything else is truthy.
@exampleexpect (user.isActive).toBeTruthy(); toBeTruthy ();
function assert(expression: any, message?: string): asserts expression@paramexpression Expression to test for truthiness.@parammessage Message to display on error. assert (import Result Result .function isSuccess<Result.Result<string, Error>>(result: Result.Result<string, Error>): result is Result.Success<string>Type guard to check if a
Result
is a
Success
.
@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@exampleimport { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = { type: 'Success', value: 10 };
if (Result.isSuccess(result)) {
console.log(result.value); // Safe access to value
}
@categoryType Guards isSuccess (const result: Result.Result<string, Error> result ));
// ❌ 値をアサーションするために Result を unwrap している
expect<string>(actual: string, message?: string): Assertion<void, string> (+1 overload) expect (import Result Result .function unwrap<Result.Success<string>>(result: Result.Success<string>): string (+3 overloads)Extracts the success value from a
Result
or
ResultAsync
.
If the input is a
Failure
, it will throw the error or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const value = Result.unwrap(result); // 42
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const value = Result.unwrap(result, 0); // 42
@exampleFailure Case (without default) - throws error
import { Result } from '@praha/byethrow';
const result = Result.fail('error');
Result.unwrap(result); // throws 'error'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result = Result.fail('error');
const value = Result.unwrap(result, 0); // 0
@seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps unwrap (const result: Result.Success<string> result )).JestAssertion<void, string>.toBe: <string>(expected: string) => voidChecks that a value is what you expect. It calls Object.is to compare values.
Don't use toBe with floating-point numbers.
@exampleexpect (result).toBe(42);
expect(status).toBe(true); toBe ('hello');#正しい例
import { import Result Result } from '@praha/byethrow';
import { const expect: ExpectStatic expect } from 'vitest';
declare const const result: Result.Result<string, Error> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <string, Error>;
// ✅ 専用のマッチャーを使っている
expect<Result.Result<string, Error>>(actual: Result.Result<string, Error>, message?: string): Assertion<void, Result.Result<string, Error>> (+1 overload) expect (const result: Result.Result<string, Error> result ).ResultMatchers<void, Result<string, Error>>.toBeSuccess: (callback?: ((value: string) => void) | undefined) => void toBeSuccess ();
expect<Result.Result<string, Error>>(actual: Result.Result<string, Error>, message?: string): Assertion<void, Result.Result<string, Error>> (+1 overload) expect (const result: Result.Result<string, Error> result ).ResultMatchers<void, Result<string, Error>>.toBeFailure: (callback?: ((error: Error) => void) | undefined) => void toBeFailure ();
// ✅ コールバックで値をアサーションしている
expect<Result.Result<string, Error>>(actual: Result.Result<string, Error>, message?: string): Assertion<void, Result.Result<string, Error>> (+1 overload) expect (const result: Result.Result<string, Error> result ).ResultMatchers<void, Result<string, Error>>.toBeSuccess: (callback?: ((value: string) => void) | undefined) => void toBeSuccess ((value: string value ) => {
expect<string>(actual: string, message?: string): Assertion<void, string> (+1 overload) expect (value: string value ).JestAssertion<void, string>.toBe: <string>(expected: string) => voidChecks that a value is what you expect. It calls Object.is to compare values.
Don't use toBe with floating-point numbers.
@exampleexpect (result).toBe(42);
expect(status).toBe(true); toBe ('hello');
});
expect<Result.Result<string, Error>>(actual: Result.Result<string, Error>, message?: string): Assertion<void, Result.Result<string, Error>> (+1 overload) expect (const result: Result.Result<string, Error> result ).ResultMatchers<void, Result<string, Error>>.toBeFailure: (callback?: ((error: Error) => void) | undefined) => void toBeFailure ((error: Error error ) => {
expect<Error>(actual: Error, message?: string): Assertion<void, Error> (+1 overload) expect (error: Error error ).JestAssertion<void, Error>.toBeInstanceOf: <ErrorConstructor>(expected: ErrorConstructor) => voidEnsure that an object is an instance of a class.
This matcher uses instanceof underneath.
@exampleexpect (new Date()).toBeInstanceOf(Date); toBeInstanceOf (var Error: ErrorConstructor Error );
});@praha/byethrow-testing のセットアップ方法はテストを参照してください。
#オプション
このルールにオプションはありません。
