For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/guide/ecosystem/linter/prefer-result-matchers.md.
  • English
  • prefer-result-matchers

    Enforces the use of the toBeSuccess() / toBeFailure() matchers from @praha/byethrow-testing in tests.

    This rule is auto-fixable. In the recommended preset, it is only enabled for test files.

    Rule Details

    Asserting on the boolean returned by Result.isSuccess() is noisy, and a failed assertion only tells you that false was not true. Calling Result.unwrap() in a test throws an unhelpful error when the result is a failure. The dedicated matchers are shorter, print the received Result when they fail, and give you a typed value in their callback.

    This rule reports the following patterns:

    • expect(Result.isSuccess(result)) / expect(Result.isFailure(result)) followed by .toBe(), .toBeTruthy(), or .toBeFalsy()
    • assert(Result.isSuccess(result)) / assert(Result.isFailure(result))
    • Result.unwrap(result) / Result.unwrapError(result)

    Incorrect

    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.@example
    import { 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>;
    // ❌ Asserting on the boolean returned by 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.@example
    import { 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) => void

    Checks 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.@example
    import { 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: () => void

    Use 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.@example
    import { 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
    ));
    // ❌ Unwrapping the result to assert on its value
    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).@example

    Success Case (without default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result); // 42
    @example

    Success Case (with default)

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    const value = Result.unwrap(result, 0); // 42
    @example

    Failure Case (without default) - throws error

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('error');
    Result.unwrap(result); // throws 'error'
    @example

    Failure 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) => void

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

    Correct

    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.@example
    import { 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>;
    // ✅ Using the dedicated matchers
    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
    ();
    // ✅ Asserting on the value in the callback
    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) => void

    Checks 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) => void

    Ensure that an object is an instance of a class. This matcher uses instanceof underneath.

    @exampleexpect (new Date()).toBeInstanceOf(Date);
    toBeInstanceOf
    (
    var Error: ErrorConstructor
    Error
    );
    });

    See Testing for how to set up @praha/byethrow-testing.

    Options

    This rule has no options.