UNPKG

@benodiwal/rusty-ts

Version:

A TypeScript library providing Rust-like Result and Option types for safer and more expressive error handling and optional values.

31 lines (26 loc) 963 B
import { describe, expect, it } from '@jest/globals'; import { Results } from '../src/result'; describe('Result', () => { it('should create an Ok result', () => { const result = Results.ok(42); expect(Results.isOk(result)).toBe(true); expect(result.value).toBe(42); }); it('should create an Err result', () => { const error = new Error('Something went wrong'); const result = Results.err(error); expect(Results.isErr(result)).toBe(true); expect(() => { throw result.error }).toThrow(); }); it('should unwrap an Ok result', () => { const result = Results.ok(42); expect(Results.unwrap(result)).toBe(42); }); it('should throw when unwrapping an Err result', () => { const error = new Error('Something went wrong'); const result = Results.err(error); expect(() => Results.unwrap(result)).toThrowError('Called unwrap on an Err value'); }); });