wellcrafted
Version:
Delightful TypeScript patterns for elegant, type-safe applications
53 lines (51 loc) • 1.83 kB
TypeScript
import { Result } from "./result-_socO0Ud.js";
import "./tap-err-C0xfVXtz.js";
import "./index-D0f5JWiT.js";
//#region src/testing.d.ts
/**
* Test-only assertion helpers for `Result` values.
*
* These helpers intentionally **throw**. A failed expectation should abort the
* test, and every test runner reports a thrown error as a failure. Tests are
* the one place throwing is the correct control flow, so these are fenced into
* the `wellcrafted/testing` entry point and kept out of `wellcrafted/result`,
* which stays throw-free. Importing from `wellcrafted/testing` in production
* code is a smell worth linting against.
*
* They are framework-agnostic: they throw a plain `Error` rather than calling
* into a specific runner, so they work under bun, vitest, jest, or
* `node:test`.
*/
/**
* Asserts that `result` is `Ok` and returns its `data`.
*
* Throws if `result` is `Err`. The returned value is narrowed to the success
* type, so no optional chaining or casting is needed at the call site.
*
* @example
* ```ts
* import { expectOk } from "wellcrafted/testing";
*
* const value = expectOk(parseConfig(raw)); // typed as the success value
* ```
*/
declare function expectOk<T>(result: Result<T, unknown>): T;
/**
* Asserts that `result` is `Err` and returns its `error`.
*
* Throws if `result` is `Ok`. The returned value is narrowed to the error
* type, so no optional chaining or casting is needed at the call site.
*
* @example
* ```ts
* import { expectErr } from "wellcrafted/testing";
*
* const error = expectErr(parseConfig("not valid"));
* expect(error.name).toBe("ConfigParseError");
* ```
*/
declare function expectErr<E>(result: Result<unknown, E>): E;
//# sourceMappingURL=testing.d.ts.map
//#endregion
export { expectErr, expectOk };
//# sourceMappingURL=testing.d.ts.map