@@ -54,20 +54,6 @@ describe('BaseException', () => {
5454 } ) ;
5555} ) ;
5656
57- describe ( 'BaseException.prototype.toString' , ( ) => {
58- it ( 'should return the name of the exception with the message, if stack is undefined' , ( ) => {
59- expect (
60- BaseException . prototype . toString . call ( {
61- message : 'An error occurred' ,
62- name : 'TestException'
63- } )
64- ) . toBe ( 'TestException: An error occurred' ) ;
65- } ) ;
66- it ( 'should return the stack if it defined' , ( ) => {
67- expect ( BaseException . prototype . toString . call ( { stack : '__STACK__' } ) ) . toContain ( '__STACK__' ) ;
68- } ) ;
69- } ) ;
70-
7157describe ( 'ExceptionBuilder' , ( ) => {
7258 it ( 'should return never for the build method if no name is specified' , ( ) => {
7359 const fn = ( ) : never => new ExceptionBuilder ( ) . build ( ) ;
@@ -131,22 +117,52 @@ describe('ExceptionBuilder', () => {
131117} ) ;
132118
133119describe ( 'ValueException' , ( ) => {
134- it ( 'should have the correct prototype' , ( ) => {
135- expect ( Object . getPrototypeOf ( ValueException ) ) . toBe ( BaseException ) ;
136- } ) ;
137- it ( 'should have the asErr static method' , ( ) => {
138- expect ( ValueException . asErr ( ) ) . toBeInstanceOf ( Err ) ;
120+ describe ( 'static' , ( ) => {
121+ it ( 'should have the correct prototype' , ( ) => {
122+ expect ( Object . getPrototypeOf ( ValueException ) ) . toBe ( BaseException ) ;
123+ } ) ;
124+ it ( 'should have the asErr static method' , ( ) => {
125+ expect ( ValueException . asErr ( ) ) . toBeInstanceOf ( Err ) ;
126+ } ) ;
127+ it ( 'should have the asAsyncErr static method' , async ( ) => {
128+ expect ( await ValueException . asAsyncErr ( ) ) . toBeInstanceOf ( Err ) ;
129+ } ) ;
139130 } ) ;
140- it ( 'should have the asAsyncErr static method' , async ( ) => {
141- expect ( await ValueException . asAsyncErr ( ) ) . toBeInstanceOf ( Err ) ;
131+ describe ( 'toErr' , ( ) => {
132+ it ( 'should return an Err instance' , ( ) => {
133+ const exception = new ValueException ( ) ;
134+ expect ( exception . toErr ( ) ) . toBeInstanceOf ( Err ) ;
135+ } ) ;
142136 } ) ;
143- it ( 'should have the toErr method' , ( ) => {
144- const exception = new ValueException ( ) ;
145- expect ( exception . toErr ( ) ) . toBeInstanceOf ( Err ) ;
137+ describe ( 'toAsyncErr' , ( ) => {
138+ it ( 'should return an Err instance' , async ( ) => {
139+ const exception = new ValueException ( ) ;
140+ expect ( await exception . toAsyncErr ( ) ) . toBeInstanceOf ( Err ) ;
141+ } ) ;
146142 } ) ;
147- it ( 'should have the asAsyncErr static method' , async ( ) => {
148- const exception = new ValueException ( ) ;
149- expect ( await exception . toAsyncErr ( ) ) . toBeInstanceOf ( Err ) ;
143+ describe ( 'toString' , ( ) => {
144+ const value = new ValueException ( 'An error occurred' , {
145+ cause : new Error ( 'Cause 1' , {
146+ cause : new Error ( 'Cause 2' )
147+ } ) ,
148+ details : {
149+ expected : 'number' ,
150+ received : 'string'
151+ }
152+ } ) . toString ( ) ;
153+ it ( 'should include the exception name and message' , ( ) => {
154+ expect ( value ) . toContain ( 'ValueException: An error occurred' ) ;
155+ } ) ;
156+ it ( 'should include the causes in reverse order' , ( ) => {
157+ const explanations = value
158+ . split ( 'The above exception was the cause of the following exception:' )
159+ . map ( ( s ) => s . trim ( ) ) ;
160+ expect ( explanations [ 0 ] ) . toMatch ( / ^ E r r o r : C a u s e 2 / ) ;
161+ expect ( explanations [ 1 ] ) . toMatch ( / ^ E r r o r : C a u s e 1 / ) ;
162+ } ) ;
163+ it ( 'should include the details' , ( ) => {
164+ expect ( value ) . toMatch ( / d e t a i l s : \s * \{ \s * e x p e c t e d : \s * ' n u m b e r ' , \s * r e c e i v e d : \s * ' s t r i n g ' \s * \} / ) ;
165+ } ) ;
150166 } ) ;
151167} ) ;
152168
0 commit comments