@@ -8,13 +8,19 @@ import {
88 CircularJSONNotSupportedError ,
99 sortTopLevelKeysForOas ,
1010 cleanColors ,
11- HandledError ,
1211 cleanArgs ,
1312 getAndValidateFileExtension ,
1413 writeToFileByExtension ,
1514} from '../utils/miscellaneous.js' ;
15+ import * as errorHandling from '../utils/error.js' ;
1616import { sanitizeLocale , sanitizePath , getPlatformSpawnArgs } from '../utils/platform.js' ;
17- import { type ResolvedApi , type Totals , ResolveError , YamlParseError } from '@redocly/openapi-core' ;
17+ import {
18+ type ResolvedApi ,
19+ type Totals ,
20+ ResolveError ,
21+ YamlParseError ,
22+ HandledError ,
23+ } from '@redocly/openapi-core' ;
1824import * as openapiCore from '@redocly/openapi-core' ;
1925import { blue , red , yellow } from 'colorette' ;
2026import * as fs from 'node:fs' ;
@@ -42,6 +48,12 @@ vi.mock('@redocly/openapi-core', async () => {
4248 stringifyYaml : vi . fn ( ( data , opts ) => data as string ) ,
4349 } ;
4450} ) ;
51+ vi . mock ( '../../utils/error.js' , async ( ) => {
52+ const actual = await vi . importActual ( '../../utils/error.js' ) ;
53+ return {
54+ ...actual ,
55+ } ;
56+ } ) ;
4557
4658describe ( 'pathToFilename' , ( ) => {
4759 it ( 'should use correct path separator' , ( ) => {
@@ -123,6 +135,7 @@ describe('getFallbackApisOrExit', () => {
123135 } ) ;
124136
125137 it ( 'should error if file from config do not exist' , async ( ) => {
138+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
126139 vi . mocked ( fs . existsSync ) . mockImplementationOnce ( ( ) => false ) ;
127140 expect . assertions ( 3 ) ;
128141 try {
@@ -131,7 +144,7 @@ describe('getFallbackApisOrExit', () => {
131144 expect ( process . stderr . write ) . toHaveBeenCalledWith (
132145 '\nsomeFile.yaml does not exist or is invalid.\n\n'
133146 ) ;
134- expect ( process . stderr . write ) . toHaveBeenCalledWith ( 'Please provide a valid path.\n\n ' ) ;
147+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith ( 'Please provide a valid path.' ) ;
135148 expect ( e . message ) . toEqual ( 'Please provide a valid path.' ) ;
136149 }
137150 } ) ;
@@ -153,6 +166,7 @@ describe('getFallbackApisOrExit', () => {
153166 } ) ;
154167
155168 it ( 'should exit with error in case if invalid path provided as args' , async ( ) => {
169+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
156170 const apisConfig = {
157171 apis : { } ,
158172 } ;
@@ -165,12 +179,13 @@ describe('getFallbackApisOrExit', () => {
165179 expect ( process . stderr . write ) . toHaveBeenCalledWith (
166180 '\nsomeFile.yaml does not exist or is invalid.\n\n'
167181 ) ;
168- expect ( process . stderr . write ) . toHaveBeenCalledWith ( 'Please provide a valid path.\n\n ' ) ;
182+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith ( 'Please provide a valid path.' ) ;
169183 expect ( e . message ) . toEqual ( 'Please provide a valid path.' ) ;
170184 }
171185 } ) ;
172186
173187 it ( 'should exit with error in case if invalid 2 path provided as args' , async ( ) => {
188+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
174189 const apisConfig = {
175190 apis : { } ,
176191 } ;
@@ -182,12 +197,13 @@ describe('getFallbackApisOrExit', () => {
182197 expect ( process . stderr . write ) . toHaveBeenCalledWith (
183198 '\nsomeFile.yaml does not exist or is invalid.\n\n'
184199 ) ;
185- expect ( process . stderr . write ) . toHaveBeenCalledWith ( 'Please provide a valid path.\n\n ' ) ;
200+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith ( 'Please provide a valid path.' ) ;
186201 expect ( e . message ) . toEqual ( 'Please provide a valid path.' ) ;
187202 }
188203 } ) ;
189204
190205 it ( 'should exit with error if only one file exist ' , async ( ) => {
206+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
191207 const apisStub = {
192208 ...apis ,
193209 notExist : {
@@ -201,16 +217,17 @@ describe('getFallbackApisOrExit', () => {
201217 . mocked ( fs . existsSync )
202218 . mockImplementation ( ( path ) => ( path as string ) . endsWith ( 'someFile.yaml' ) ) ;
203219
204- expect . assertions ( 4 ) ;
220+ expect . assertions ( 5 ) ;
205221
206222 try {
207223 await getFallbackApisOrExit ( undefined , configStub ) ;
208224 } catch ( e ) {
209225 expect ( process . stderr . write ) . toHaveBeenCalledWith (
210226 '\nnotExist.yaml does not exist or is invalid.\n\n'
211227 ) ;
212- expect ( process . stderr . write ) . toHaveBeenCalledWith ( 'Please provide a valid path.\n\n' ) ;
213- expect ( process . stderr . write ) . toHaveBeenCalledTimes ( 2 ) ;
228+ expect ( process . stderr . write ) . toHaveBeenCalledTimes ( 1 ) ;
229+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith ( 'Please provide a valid path.' ) ;
230+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledTimes ( 1 ) ;
214231 expect ( e . message ) . toEqual ( 'Please provide a valid path.' ) ;
215232 }
216233 existSyncMock . mockClear ( ) ;
@@ -408,46 +425,49 @@ describe('handleErrors', () => {
408425 } ) ;
409426
410427 it ( 'should handle ResolveError' , ( ) => {
428+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
411429 const resolveError = new ResolveError ( new Error ( 'File not found.' ) ) ;
412430 expect ( ( ) => handleError ( resolveError , ref ) ) . toThrowError ( HandledError ) ;
413- expect ( redColoretteMocks ) . toHaveBeenCalledTimes ( 1 ) ;
414- expect ( process . stderr . write ) . toHaveBeenCalledWith (
415- `Failed to resolve API description at openapi/test.yaml:\n\n - File not found.\n\n`
431+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith (
432+ `Failed to resolve API description at openapi/test.yaml:\n\n - File not found.`
416433 ) ;
417434 } ) ;
418435
419436 it ( 'should handle YamlParseError' , ( ) => {
437+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
420438 const yamlParseError = new YamlParseError ( new Error ( 'Invalid yaml.' ) , { } as any ) ;
421439 expect ( ( ) => handleError ( yamlParseError , ref ) ) . toThrowError ( HandledError ) ;
422- expect ( redColoretteMocks ) . toHaveBeenCalledTimes ( 1 ) ;
423- expect ( process . stderr . write ) . toHaveBeenCalledWith (
424- `Failed to parse API description at openapi/test.yaml:\n\n - Invalid yaml.\n\n`
440+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith (
441+ `Failed to parse API description at openapi/test.yaml:\n\n - Invalid yaml.`
425442 ) ;
426443 } ) ;
427444
428445 it ( 'should handle CircularJSONNotSupportedError' , ( ) => {
446+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
429447 const circularError = new CircularJSONNotSupportedError ( new Error ( 'Circular json' ) ) ;
430448 expect ( ( ) => handleError ( circularError , ref ) ) . toThrowError ( HandledError ) ;
431- expect ( process . stderr . write ) . toHaveBeenCalledWith (
449+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith (
432450 `Detected circular reference which can't be converted to JSON.\n` +
433- `Try to use ${ blue ( 'yaml' ) } output or remove ${ blue ( '--dereferenced' ) } .\n\n `
451+ `Try to use ${ blue ( 'yaml' ) } output or remove ${ blue ( '--dereferenced' ) } .`
434452 ) ;
435453 } ) ;
436454
437455 it ( 'should handle SyntaxError' , ( ) => {
456+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
438457 const testError = new SyntaxError ( 'Unexpected identifier' ) ;
439458 testError . stack = 'test stack' ;
440459 expect ( ( ) => handleError ( testError , ref ) ) . toThrowError ( HandledError ) ;
441- expect ( process . stderr . write ) . toHaveBeenCalledWith (
442- 'Syntax error: Unexpected identifier test stack\n\n '
460+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith (
461+ 'Syntax error: Unexpected identifier test stack'
443462 ) ;
444463 } ) ;
445464
446465 it ( 'should throw unknown error' , ( ) => {
466+ vi . spyOn ( errorHandling , 'exitWithError' ) ;
447467 const testError = new Error ( 'Test error.' ) ;
448468 expect ( ( ) => handleError ( testError , ref ) ) . toThrowError ( HandledError ) ;
449- expect ( process . stderr . write ) . toHaveBeenCalledWith (
450- `Something went wrong when processing openapi/test.yaml:\n\n - Test error.\n\n `
469+ expect ( errorHandling . exitWithError ) . toHaveBeenCalledWith (
470+ `Something went wrong when processing openapi/test.yaml:\n\n - Test error.`
451471 ) ;
452472 } ) ;
453473} ) ;
0 commit comments