From c7f93ccb2c9b3564e4b1e795fd8d3f2204ae1b1f Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Tue, 18 Aug 2026 00:16:38 +0000 Subject: [PATCH] [Tests] Cover Result.valueOrAbort and doOnOk error handling --- .../cli-kit/src/public/node/result.test.ts | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/cli-kit/src/public/node/result.test.ts b/packages/cli-kit/src/public/node/result.test.ts index 67486603925..402e9763ab7 100644 --- a/packages/cli-kit/src/public/node/result.test.ts +++ b/packages/cli-kit/src/public/node/result.test.ts @@ -1,7 +1,8 @@ import {err, ok} from './result.js' import {mockAndCaptureOutput} from './testing/output.js' import {outputSuccess} from './output.js' -import {describe, expect, test} from 'vitest' +import {AbortError, BugError} from './error.js' +import {describe, expect, test, vi} from 'vitest' describe('ok', () => { test('create ok with value', () => { @@ -41,6 +42,48 @@ describe('valueOrBug', () => { }) }) +describe('valueOrAbort', () => { + test('when ok result should return value', () => { + // When + const result = ok(123).valueOrAbort() + + // Then + expect(result).toEqual(123) + }) + + test('when err result with FatalError should throw the original FatalError', () => { + // Given + const fatal = new BugError('fatal bug') + + // When / Then + expect(() => err(fatal).valueOrAbort()).toThrow(fatal) + }) + + test('when err result with standard Error should throw AbortError with matching message and stack', () => { + // Given + const standardError = new Error('standard error') + standardError.stack = 'custom stack trace' + + // When / Then + try { + err(standardError).valueOrAbort() + expect.fail('Expected valueOrAbort to throw') + } catch (error) { + if (error instanceof AbortError) { + expect(error.message).toEqual('standard error') + expect(error.stack).toEqual('custom stack trace') + } else { + throw error + } + } + }) + + test('when err result with non-Error value should throw AbortError with string representation', () => { + // When / Then + expect(() => err('string error').valueOrAbort()).toThrow(new AbortError('string error')) + }) +}) + describe('mapError', () => { test('when ok result should not affect the result', () => { // When @@ -71,6 +114,18 @@ describe('doOnOk', () => { expect(!result.isErr() && result.value).toEqual(123) expect(outpuMocker.success()).toMatchInlineSnapshot('"result ok 123"') }) + + test('when err result should not execute the command and return err', () => { + // Given + const handler = vi.fn() + + // When + const result = err(new Error('error')).doOnOk(handler) + + // Then + expect(handler).not.toHaveBeenCalled() + expect(result.isErr() && result.error).toEqual(new Error('error')) + }) }) describe('map', () => {