1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * expect a function to throw Error 18 * @param actualValue 19 * @returns {{pass: boolean, message: string}} 20 */ 21function assertThrowError (actualValue, expected) { 22 let result = false 23 let err 24 if (typeof actualValue !== 'function') { 25 throw new Error('actualValue is not a function') 26 } 27 try { 28 actualValue() 29 return { 30 pass: result, 31 message: ' An error is not thrown while it is expected!' 32 } 33 } catch (e) { 34 err = e 35 } 36 37 if (err instanceof Error) { 38 console.log(err.message) 39 if (err.message == expected[0]) { 40 result = true 41 } 42 } 43 return { 44 pass: result, 45 message: 'expected throw failed , ' + err.message + ' is not ' + expected[0] 46 } 47} 48 49export default assertThrowError 50