@@ -19,40 +19,86 @@ class CustomError extends Error {
1919
2020describe ( "[Unit] FunctionAssertion.test.ts" , ( ) => {
2121 describe ( ".toThrow" , ( ) => {
22- context ( "when the function throws" , ( ) => {
23- const variants = [
24- - 1 ,
25- "foo" ,
26- true ,
27- null ,
28- Error ( ) ,
29- new CustomError ( "bar" )
30- ] as const ;
31-
32- variants . forEach ( error => {
33- it ( `[${ error } ] returns the assertion instance` , ( ) => {
34- const test = new FunctionAssertion ( ( ) => {
35- throw error ;
22+ context ( "when the error param is not present" , ( ) => {
23+ context ( "and the function throws" , ( ) => {
24+ const variants = [
25+ - 1 ,
26+ "foo" ,
27+ true ,
28+ null ,
29+ Error ( ) ,
30+ new CustomError ( "bar" )
31+ ] as const ;
32+
33+ variants . forEach ( error => {
34+ it ( `[${ error } ] returns the assertion instance` , ( ) => {
35+ const test = new FunctionAssertion ( ( ) => {
36+ throw error ;
37+ } ) ;
38+
39+ assert . deepStrictEqual ( test . toThrow ( ) , test ) ;
40+ assert . throws ( ( ) => test . not . toThrow ( ) , {
41+ message : "Expected the function NOT to throw when called" ,
42+ name : AssertionError . name
43+ } ) ;
3644 } ) ;
45+ } ) ;
46+ } ) ;
47+
48+ context ( "and the function does not throw" , ( ) => {
49+ it ( "throws an assertion error" , ( ) => {
50+ const test = new FunctionAssertion ( ( ) => undefined ) ;
3751
38- assert . deepStrictEqual ( test . toThrow ( ) , test ) ;
39- assert . throws ( ( ) => test . not . toThrow ( ) , {
40- message : "Expected the function NOT to throw when called" ,
52+ assert . throws ( ( ) => test . toThrow ( ) , {
53+ message : "Expected the function to throw when called" ,
4154 name : AssertionError . name
4255 } ) ;
56+ assert . deepStrictEqual ( test . not . toThrow ( ) , test ) ;
4357 } ) ;
4458 } ) ;
4559 } ) ;
4660
47- context ( "when the function does not throw" , ( ) => {
48- it ( "throws an assertion error" , ( ) => {
49- const test = new FunctionAssertion ( ( ) => undefined ) ;
61+ context ( "when the error param is present" , ( ) => {
62+ context ( "and the function throws" , ( ) => {
63+ context ( "and the thrown error is strictly equal to the param" , ( ) => {
64+ it ( "returns the assertion instance" , ( ) => {
65+ const test = new FunctionAssertion ( ( ) => {
66+ throw new Error ( "This is expected!" ) ;
67+ } ) ;
5068
51- assert . throws ( ( ) => test . toThrow ( ) , {
52- message : "Expected the function to throw when called" ,
53- name : AssertionError . name
69+ assert . deepStrictEqual ( test . toThrow ( new Error ( "This is expected!" ) ) , test ) ;
70+ assert . throws ( ( ) => test . not . toThrow ( new Error ( "This is expected!" ) ) , {
71+ message : "Expected the function NOT to throw - Error: This is expected!" ,
72+ name : AssertionError . name
73+ } ) ;
74+ } ) ;
75+ } ) ;
76+
77+ context ( "and the error is not strictly equal to the param" , ( ) => {
78+ it ( "throws and assertion error" , ( ) => {
79+ const test = new FunctionAssertion ( ( ) => {
80+ throw new Error ( "This is expected!" ) ;
81+ } ) ;
82+
83+ assert . throws ( ( ) => test . toThrow ( new Error ( "Another error here!" ) ) , {
84+ message : "Expected the function to throw - Error: Another error here!" ,
85+ name : AssertionError . name
86+ } ) ;
87+ assert . deepStrictEqual ( test . not . toThrow ( new Error ( "Another error here!" ) ) , test ) ;
88+ } ) ;
89+ } ) ;
90+ } ) ;
91+
92+ context ( "and the function does not throw" , ( ) => {
93+ it ( "throw an assertion error" , ( ) => {
94+ const test = new FunctionAssertion ( ( ) => undefined ) ;
95+
96+ assert . throws ( ( ) => test . toThrow ( new Error ( "Unreachable!" ) ) , {
97+ message : "Expected the function to throw - Error: Unreachable!" ,
98+ name : AssertionError . name
99+ } ) ;
100+ assert . deepStrictEqual ( test . not . toThrow ( new Error ( "Unreachable!" ) ) , test ) ;
54101 } ) ;
55- assert . deepStrictEqual ( test . not . toThrow ( ) , test ) ;
56102 } ) ;
57103 } ) ;
58104 } ) ;
0 commit comments