The _unsafeUnwrap() and _unsafeUnwrapErr() methods are designed exclusively for testing. They extract the inner value from a Result without proper error handling, throwing an error if the wrong variant is accessed.
Test Environments OnlyThese methods should never be used in production code. The underscore prefix is a convention indicating they are unsafe. In production, always use safe methods like match(), unwrapOr(), or proper error handling.
import { expect, test } from 'vitest'import { calculateTotal } from './calculator'test('calculates total correctly', () => { const result = calculateTotal([10, 20, 30]) expect(result._unsafeUnwrap()).toBe(60)})test('returns zero for empty array', () => { const result = calculateTotal([]) expect(result._unsafeUnwrap()).toBe(0)})
import { expect, test } from 'vitest'import { safeTry, ok, err } from 'neverthrow'test('safeTry returns first error', () => { const result = safeTry(function*() { const a = yield* ok(1) const b = yield* err('SecondError') const c = yield* ok(3) return ok(a + b + c) }) expect(result._unsafeUnwrapErr()).toBe('SecondError')})test('safeTry returns ok when all succeed', () => { const result = safeTry(function*() { const a = yield* ok(1) const b = yield* ok(2) return ok(a + b) }) expect(result._unsafeUnwrap()).toBe(3)})
import { expect, test } from 'vitest'import { processData } from './processor'test('processes data successfully', () => { const result = processData('input') // Use custom matchers if available expect(result.isOk()).toBe(true) // Or unwrap for detailed assertions const data = result._unsafeUnwrap() expect(data).toHaveProperty('processed', true) expect(data.value).toBeGreaterThan(0)})