1/*
2 * Copyright (c) 2021-2024 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
16function assertThrowError(actualValue, expected) {
17    let result = false;
18    let message = '';
19    let err;
20    if (typeof actualValue !== 'function') {
21        throw new Error('actualValue is not a function');
22    }
23    try {
24        actualValue();
25        return {
26            pass: result,
27            message: ' An error is not thrown while it is expected!'
28        };
29    } catch (e) {
30        err = e;
31    }
32    if (err instanceof Error) {
33        let type = typeof expected[0];
34        if (type === 'function') {
35            result = err.constructor.name === expected[0].name;
36            message = 'expected throw failed , ' + err.constructor.name + ' is not ' + expected[0].name;
37        } else if (type === 'string') {
38            result = err.message.includes(expected[0]);
39            message = 'expected throw failed , ' + err.message + ' is not ' + expected[0];
40        }
41    }
42    return {
43        pass: result,
44        message: message
45    };
46}
47
48export default assertThrowError;
49