1- import { doesNotThrow , equal , ok } from 'assert' ;
21import ErrorSubclass from '../src/ErrorSubclass' ;
32
43const ERROR_MESSAGE = '__MESSAGE__' ;
@@ -9,54 +8,46 @@ class SubSubError extends ErrorSubclass {
98
109const { captureStackTrace} = Error ;
1110
12- const tests = {
13- ErrorSubclass : {
14- 'Subclass of ErrorSubclass' : {
15- 'can be instantiated' : ( ) => {
16- doesNotThrow ( ( ) => new SubSubError ( ERROR_MESSAGE ) ) ;
17- } ,
18-
19- 'SubSubclass instance' : {
20- afterEach ( ) {
21- Error . captureStackTrace = captureStackTrace ;
22- } ,
23-
24- 'should be instanceof Error' : ( ) => {
25- const instance = new SubSubError ( ERROR_MESSAGE ) ;
26- ok ( instance instanceof Error ) ;
27- ok ( instance instanceof SubSubError ) ;
28- } ,
29-
30- 'should have a name that matches the displayName' : ( ) => {
31- const instance = new SubSubError ( ERROR_MESSAGE ) ;
32- equal ( instance . name , 'SubSubErrorDisplayName' ) ;
33- } ,
34-
35- 'should have a message' : ( ) => {
36- const instance = new SubSubError ( ERROR_MESSAGE ) ;
37- equal ( instance . message , ERROR_MESSAGE ) ;
38- } ,
39-
40- 'has a toString() method' : ( ) => {
41- const instance = new SubSubError ( ERROR_MESSAGE ) ;
42- equal ( typeof instance . toString , 'function' ) ;
43- equal ( instance . toString ( ) , `SubSubErrorDisplayName: ${ ERROR_MESSAGE } ` ) ;
44- } ,
45-
46- 'when captureStackTrace is supported, should have a stack' : ( ) => {
47- equal ( typeof Error . captureStackTrace , 'function' ) ;
48- const instance = new SubSubError ( ERROR_MESSAGE ) ;
49- ok ( instance . stack ) ;
50- equal ( typeof instance . stack , 'string' ) ;
51- ok ( instance . stack . match ( `SubSubErrorDisplayName: ${ ERROR_MESSAGE } ` ) ) ;
52- }
53-
54- // 'when captureStackTrace is not supported, should have a stack': () => {
55- // // TODO: how to test?
56- // },
57- }
58- }
59- }
60- } ;
61-
62- export default tests ;
11+ afterEach ( ( ) => {
12+ Error . captureStackTrace = captureStackTrace ;
13+ } ) ;
14+
15+ test ( 'Subclass of ErrorSubclass can be instantiated' , ( ) => {
16+ expect ( ( ) => new SubSubError ( ERROR_MESSAGE ) ) . not . toThrow ( ) ;
17+ } ) ;
18+
19+ test ( 'SubSubclass instance should be instanceof Error' , ( ) => {
20+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
21+ expect ( instance ) . toBeInstanceOf ( Error ) ;
22+ expect ( instance ) . toBeInstanceOf ( SubSubError ) ;
23+ } ) ;
24+
25+ test ( 'SubSubclass instance should have a name that matches the displayName' , ( ) => {
26+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
27+ expect ( instance . name ) . toBe ( 'SubSubErrorDisplayName' ) ;
28+ } ) ;
29+
30+ test ( 'SubSubclass instance should have a message' , ( ) => {
31+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
32+ expect ( instance . message ) . toBe ( ERROR_MESSAGE ) ;
33+ } ) ;
34+
35+ test ( 'SubSubclass instance has a toString() method' , ( ) => {
36+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
37+ expect ( typeof instance . toString ) . toBe ( 'function' ) ;
38+ expect ( instance . toString ( ) ) . toBe ( `SubSubErrorDisplayName: ${ ERROR_MESSAGE } ` ) ;
39+ } ) ;
40+
41+ test ( 'SubSubclass instance when captureStackTrace is supported, should have a stack' , ( ) => {
42+ expect ( typeof Error . captureStackTrace ) . toBe ( 'function' ) ;
43+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
44+ expect ( typeof instance . stack ) . toBe ( 'string' ) ;
45+ expect ( instance . stack ) . toMatch ( `SubSubErrorDisplayName: ${ ERROR_MESSAGE } ` ) ;
46+ } ) ;
47+
48+ test ( 'SubSubclass, when captureStackTrace is not supported, should still have a stack' , ( ) => {
49+ Reflect . deleteProperty ( Error , 'captureStackTrace' ) ;
50+ expect ( Error . captureStackTrace ) . not . toBeDefined ( ) ;
51+ const instance = new SubSubError ( ERROR_MESSAGE ) ;
52+ expect ( typeof instance . stack ) . toBe ( 'string' ) ;
53+ } ) ;
0 commit comments