• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import { AssertResult } from '../modal/assertModel';
17import { AnyType } from '../types/common';
18
19function assertPromiseIsRejectedWith(
20  actualPromise: Promise<AnyType>,
21  expectedValue: AnyType[]
22): Promise<AssertResult> {
23  const tips = (passed: boolean) => {
24    return (
25      'Expected a promise ' +
26      (passed ? 'not ' : '') +
27      'to be rejected with ' +
28      JSON.stringify(expectedValue[0])
29    );
30  };
31  return actualPromise.then<AssertResult, AssertResult>(
32    (): AssertResult => {
33      return {
34        pass: false,
35        message: tips(false) + ' but actualValue is resolve',
36      };
37    },
38    (actualValue: AnyType): AssertResult => {
39      if (JSON.stringify(actualValue) == JSON.stringify(expectedValue[0])) {
40        return {
41          pass: true,
42          message:
43            'actualValue was rejected with ' +
44            JSON.stringify(actualValue) +
45            '.',
46        };
47      } else if (
48        actualValue instanceof Error &&
49        typeof expectedValue[0] === 'string' &&
50        (actualValue as Error).message === expectedValue[0]
51      ) {
52        return {
53          pass: true,
54          message:
55            'actualValue was rejected with ' +
56            (actualValue as Error).message +
57            '.',
58        };
59      } else {
60        return {
61          pass: false,
62          message:
63            tips(false) +
64            ' but it was rejected with ' +
65            JSON.stringify(actualValue) +
66            '.',
67        };
68      }
69    }
70  );
71}
72export { assertPromiseIsRejectedWith };
73