@@ -55,15 +55,50 @@ describe("HardhatError helpers", () => {
5555 } ) ;
5656 } ) ;
5757
58- it ( "Should not throw if the error is a HardhatError with the same descriptor and message arguments" , ( ) => {
59- assertIsHardhatError (
60- new HardhatError (
58+ describe ( "Error cause" , ( ) => {
59+ it ( "Should not throw if the error cause is provided and its the same" , ( ) => {
60+ const cause = new Error ( "cause" ) ;
61+
62+ assertIsHardhatError (
63+ new HardhatError (
64+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
65+ { option : "foo" , task : "bar" } ,
66+ cause ,
67+ ) ,
6168 HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
6269 { option : "foo" , task : "bar" } ,
63- ) ,
64- HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
65- { option : "foo" , task : "bar" } ,
66- ) ;
70+ cause ,
71+ ) ;
72+ } ) ;
73+
74+ it ( "Should throw if a cause is provided and doesn't match the error cause (by reference)" , ( ) => {
75+ assert . throws ( ( ) => {
76+ assertIsHardhatError (
77+ new HardhatError (
78+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
79+ { option : "foo" , task : "bar" } ,
80+ new Error ( "cause" ) ,
81+ ) ,
82+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
83+ { option : "foo" , task : "bar" } ,
84+ new Error ( "cause" ) ,
85+ ) ;
86+ } ) ;
87+ } ) ;
88+
89+ it ( "Should throw if the cause is provided but there's none" , ( ) => {
90+ assert . throws ( ( ) => {
91+ assertIsHardhatError (
92+ new HardhatError (
93+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
94+ { option : "foo" , task : "bar" } ,
95+ ) ,
96+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
97+ { option : "foo" , task : "bar" } ,
98+ new Error ( "cause" ) ,
99+ ) ;
100+ } ) ;
101+ } ) ;
67102 } ) ;
68103 } ) ;
69104
@@ -103,6 +138,24 @@ describe("HardhatError helpers", () => {
103138 { option : "foo" , task : "bar" } ,
104139 ) ;
105140 } ) ;
141+
142+ it ( "asserts the cause" , ( ) => {
143+ assert . throws ( ( ) => {
144+ assertThrowsHardhatError (
145+ ( ) => {
146+ throw new HardhatError (
147+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
148+ { option : "foo" , task : "bar" } ,
149+ new Error ( "cause" ) ,
150+ ) ;
151+ } ,
152+
153+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
154+ { option : "foo" , task : "bar" } ,
155+ new Error ( "cause" ) ,
156+ ) ;
157+ } ) ;
158+ } ) ;
106159 } ) ;
107160
108161 describe ( "assertRejectsWithHardhatError" , ( ) => {
@@ -164,5 +217,22 @@ describe("HardhatError helpers", () => {
164217 { option : "foo" , task : "bar" } ,
165218 ) ;
166219 } ) ;
220+
221+ it ( "asserts the cause" , async ( ) => {
222+ await assert . rejects ( async ( ) => {
223+ await assertRejectsWithHardhatError (
224+ async ( ) => {
225+ throw new HardhatError (
226+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
227+ { option : "foo" , task : "bar" } ,
228+ new Error ( "cause" ) ,
229+ ) ;
230+ } ,
231+ HardhatError . ERRORS . CORE . TASK_DEFINITIONS . UNRECOGNIZED_TASK_OPTION ,
232+ { option : "foo" , task : "bar" } ,
233+ new Error ( "cause" ) ,
234+ ) ;
235+ } ) ;
236+ } ) ;
167237 } ) ;
168238} ) ;
0 commit comments