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 { ServiceAttrIF, AssertMatcherIF, ApiIF } from '../../interface'; 17import { StaticSpec } from './StaticSpec'; 18import { StaticSuite } from './StaticSuite'; 19import { TAG } from '../../Constant'; 20import { Core } from '../../core'; 21import { AssertException } from './AssertException'; 22import { LogExpectError } from '../report/LogExpectError'; 23import { AnyType, NumberType } from '../types/common'; 24import { AssertResult } from '../modal/assertModel'; 25import { SpecService } from './SpecService'; 26import { SuiteService } from './SuiteService'; 27 28class ExpectService { 29 public id: string; 30 public matchers: AssertMatcherIF | null; 31 public customMatchers: Array<string>; 32 public coreContext: Core | null; 33 constructor(attr: ServiceAttrIF) { 34 this.id = attr.id; 35 this.matchers = null; 36 this.customMatchers = new Array<string>(); 37 this.coreContext = null; 38 } 39 40 expect(actualValue?: AnyType): AssertMatcher { 41 const core = this.coreContext; 42 let currentRunningSpec = new StaticSpec({ description: '' }); 43 let currentRunningSuite = new StaticSuite({ description: '' }); 44 if (core) { 45 const specService = core.getDefaultService('spec'); 46 const suiteService = core.getDefaultService('suite'); 47 if (specService !== null) { 48 const spec = (specService as SpecService).getCurrentRunningSpec(); 49 if (spec) { 50 currentRunningSpec = spec; 51 } 52 } 53 if (suiteService !== null) { 54 currentRunningSuite = (suiteService as SuiteService).getCurrentRunningSuite(); 55 } 56 } 57 return new AssertMatcher(actualValue, currentRunningSpec, currentRunningSuite, this.matchers); 58 } 59 60 init(coreContext: Core) { 61 this.coreContext = coreContext; 62 } 63 64 addMatchers(matchers: AssertMatcherIF) { 65 this.matchers = matchers; 66 } 67 68 // @todo 不支持 69 removeMatchers(customAssertionName: string) {} 70 71 apis(): ApiIF { 72 const _this = this; 73 return { 74 name: 'ExpectService', 75 expect: (actualValue?: AnyType) => { 76 return _this.expect(actualValue); 77 }, 78 }; 79 } 80} 81 82export class AssertMatcher { 83 public actualValue: AnyType; 84 public isNot: Boolean; 85 public currentRunningSuite: StaticSuite; 86 public currentRunningSpec: StaticSpec; 87 public assertClose: (expectValue: NumberType, precision: NumberType) => void; 88 public assertContain: (expectValue: AnyType) => void; 89 public assertFail: () => void; 90 public assertTrue: () => void; 91 public assertFalse: () => void; 92 public assertInstanceOf: (expectValue: string) => void; 93 public assertLarger: (expectValue: NumberType) => void; 94 public assertLess: (expectValue: NumberType) => void; 95 public assertNull: () => void; 96 public assertThrow: (expectValue: string) => void; 97 public assertThrowError: (expectValue: string, expect?: string) => void; 98 public assertUndefined: () => void; 99 public assertLargerOrEqual: (expectValue: NumberType) => void; 100 public assertLessOrEqual: (expectValue: NumberType) => void; 101 public assertNaN: () => void; 102 public assertNegUnlimited: () => void; 103 public assertPosUnlimited: () => void; 104 public assertDeepEquals: (expectValue: AnyType) => void; 105 public assertEqual: (expectValue: AnyType) => void; 106 public assertPromiseIsPending: () => Promise<void>; 107 public assertPromiseIsRejected: () => Promise<void>; 108 public assertPromiseIsRejectedWith: (expectValue: AnyType) => Promise<void>; 109 public assertPromiseIsRejectedWithError: (expectValue: string, expectValue2?: string) => Promise<void>; 110 public assertPromiseIsResolved: () => Promise<void>; 111 public assertPromiseIsResolvedWith: (expectValue: string) => Promise<void>; 112 constructor( 113 actualValue: AnyType, 114 currentRunningSpec: StaticSpec, 115 currentRunningSuite: StaticSuite, 116 matchers: AssertMatcherIF | null 117 ) { 118 this.isNot = false; 119 this.currentRunningSpec = currentRunningSpec; 120 this.currentRunningSuite = currentRunningSuite; 121 this.actualValue = actualValue; 122 this.assertClose = (expectValue: NumberType, precision: NumberType) => {}; 123 this.assertContain = (expectValue: AnyType) => {}; 124 this.assertFail = () => {}; 125 this.assertTrue = () => {}; 126 this.assertFalse = () => {}; 127 this.assertLarger = (expectValue: NumberType) => {}; 128 this.assertLess = (expectValue: NumberType) => {}; 129 this.assertLargerOrEqual = (expectValue: NumberType) => {}; 130 this.assertLessOrEqual = (expectValue: NumberType) => {}; 131 this.assertUndefined = () => {}; 132 this.assertNull = () => {}; 133 this.assertThrow = (expectValue: string) => {}; 134 this.assertThrowError = (expectValue: string, expect?: string) => {}; 135 this.assertNaN = () => {}; 136 this.assertNegUnlimited = () => {}; 137 this.assertPosUnlimited = () => {}; 138 this.assertDeepEquals = (expectValue: AnyType) => {}; 139 this.assertEqual = (expectValue: AnyType) => {}; 140 this.assertInstanceOf = (expectValue: string) => {}; 141 this.assertPromiseIsPending = () => { 142 return Promise.resolve(); 143 }; 144 this.assertPromiseIsRejected = () => { 145 return Promise.resolve(); 146 }; 147 this.assertPromiseIsResolved = () => { 148 return Promise.resolve(); 149 }; 150 this.assertPromiseIsRejectedWith = (expectValue: AnyType) => { 151 return Promise.resolve(); 152 }; 153 this.assertPromiseIsResolvedWith = (expectValue: AnyType) => { 154 return Promise.resolve(); 155 }; 156 this.assertPromiseIsRejectedWithError = (expectValue: string, expectValue2?: string) => { 157 return Promise.resolve(); 158 }; 159 if (matchers !== null) { 160 this.addAssert(matchers); 161 } 162 } 163 // 翻转方法 164 not() { 165 this.isNot = true; 166 return this; 167 } 168 message(msg: string) { 169 this.currentRunningSpec.expectMsg = msg; 170 console.info(`${TAG} msg: ${msg}`); 171 return this; 172 } 173 174 addCloseAssert(matchers: AssertMatcherIF) { 175 const numFn = (matcherName: string, assertFn: (actualValue: number, expected: number[]) => AssertResult) => { 176 return (arg1: NumberType, arg2: NumberType) => { 177 try { 178 const result = assertFn(this.actualValue as number, [arg1 as number, arg2 as number]); 179 if (this.isNot) { 180 result.pass = !result.pass; 181 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg1, result.message); 182 } 183 if (!result.pass) { 184 const assertError = new AssertException(result.message); 185 const currentRunningSpec = this.currentRunningSpec; 186 const currentRunningSuite = this.currentRunningSuite; 187 if (currentRunningSpec) { 188 currentRunningSpec.fail = assertError; 189 } else { 190 currentRunningSuite.hookError = assertError; 191 } 192 throw assertError; 193 } 194 } catch (error: Error) { 195 const currentRunningSpec = this.currentRunningSpec; 196 const currentRunningSuite = this.currentRunningSuite; 197 if (currentRunningSpec) { 198 currentRunningSpec.fail = error; 199 } else { 200 currentRunningSuite.hookError = error; 201 } 202 } 203 }; 204 }; 205 const assertClose = matchers.assertClose; 206 207 if (assertClose) { 208 this.assertClose = numFn('assertClose', assertClose); 209 } 210 } 211 212 addNumAssert(matchers: AssertMatcherIF) { 213 const numFn = (matcherName: string, assertFn: (actualValue: number, expected: number[]) => AssertResult) => { 214 return (arg1: NumberType): void => { 215 try { 216 const result = assertFn(this.actualValue as number, [arg1 as number]); 217 if (this.isNot) { 218 result.pass = !result.pass; 219 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg1, result.message); 220 } 221 if (!result.pass) { 222 const assertError = new AssertException(result.message); 223 const currentRunningSpec = this.currentRunningSpec; 224 const currentRunningSuite = this.currentRunningSuite; 225 if (currentRunningSpec) { 226 currentRunningSpec.fail = assertError; 227 } else { 228 currentRunningSuite.hookError = assertError; 229 } 230 throw assertError; 231 } 232 } catch (error: Error) { 233 const currentRunningSpec = this.currentRunningSpec; 234 const currentRunningSuite = this.currentRunningSuite; 235 if (currentRunningSpec) { 236 currentRunningSpec.fail = error; 237 } else { 238 currentRunningSuite.hookError = error; 239 } 240 } 241 }; 242 }; 243 244 const assertLarger = matchers.assertLarger; 245 if (assertLarger) { 246 this.assertLarger = numFn('assertLarger', assertLarger); 247 } 248 249 const assertLess = matchers.assertLess; 250 if (assertLess) { 251 this.assertLess = numFn('assertLess', assertLess); 252 } 253 254 const assertLargerOrEqual = matchers.assertLargerOrEqual; 255 if (assertLargerOrEqual) { 256 this.assertLargerOrEqual = numFn('assertLargerOrEqual', assertLargerOrEqual); 257 } 258 259 const assertLessOrEqual = matchers.assertLessOrEqual; 260 if (assertLessOrEqual) { 261 this.assertLessOrEqual = numFn('assertLessOrEqual', assertLessOrEqual); 262 } 263 } 264 265 addFailAssert(matchers: AssertMatcherIF) { 266 const voidFn = (matcherName: string, assertFn: () => AssertResult) => { 267 return () => { 268 try { 269 const result = assertFn(); 270 if (this.isNot) { 271 result.pass = !result.pass; 272 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, 'undefined', result.message); 273 } 274 if (!result.pass) { 275 const assertError = new AssertException(result.message); 276 const currentRunningSpec = this.currentRunningSpec; 277 const currentRunningSuite = this.currentRunningSuite; 278 if (currentRunningSpec) { 279 currentRunningSpec.fail = assertError; 280 } else { 281 currentRunningSuite.hookError = assertError; 282 } 283 throw assertError; 284 } 285 } catch (error: Error) { 286 const currentRunningSpec = this.currentRunningSpec; 287 const currentRunningSuite = this.currentRunningSuite; 288 if (currentRunningSpec) { 289 currentRunningSpec.fail = error; 290 } else { 291 currentRunningSuite.hookError = error; 292 } 293 } 294 }; 295 }; 296 297 const assertFail = matchers.assertFail; 298 if (assertFail) { 299 this.assertFail = voidFn('assertFail', assertFail); 300 } 301 } 302 303 addNumWithoutExpectAssert(matchers: AssertMatcherIF) { 304 const nNumFn = (matcherName: string, assertFn: (actualValue: number) => AssertResult) => { 305 return () => { 306 try { 307 const result = assertFn(this.actualValue as number); 308 if (this.isNot) { 309 result.pass = !result.pass; 310 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, 'undefined', result.message); 311 } 312 if (!result.pass) { 313 const assertError = new AssertException(result.message); 314 const currentRunningSpec = this.currentRunningSpec; 315 const currentRunningSuite = this.currentRunningSuite; 316 if (currentRunningSpec) { 317 currentRunningSpec.fail = assertError; 318 } else { 319 currentRunningSuite.hookError = assertError; 320 } 321 throw assertError; 322 } 323 } catch (error: Error) { 324 const currentRunningSpec = this.currentRunningSpec; 325 const currentRunningSuite = this.currentRunningSuite; 326 if (currentRunningSpec) { 327 currentRunningSpec.fail = error; 328 } else { 329 currentRunningSuite.hookError = error; 330 } 331 } 332 }; 333 }; 334 const assertNaN = matchers.assertNaN; 335 if (assertNaN) { 336 this.assertNaN = nNumFn('assertNaN', assertNaN); 337 } 338 339 const assertNegUnlimited = matchers.assertNegUnlimited; 340 if (assertNegUnlimited) { 341 this.assertNegUnlimited = nNumFn('assertNegUnlimited', assertNegUnlimited); 342 } 343 344 const assertPosUnlimited = matchers.assertPosUnlimited; 345 if (assertPosUnlimited) { 346 this.assertPosUnlimited = nNumFn('assertPosUnlimited', assertPosUnlimited); 347 } 348 } 349 350 addBooleanAssert(matchers: AssertMatcherIF) { 351 const booleanFn = (matcherName: string, assertFn: (actualValue: boolean) => AssertResult) => { 352 return () => { 353 try { 354 const result = assertFn(this.actualValue as boolean); 355 if (this.isNot) { 356 result.pass = !result.pass; 357 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, 'undefined', result.message); 358 } 359 if (!result.pass) { 360 const assertError = new AssertException(result.message); 361 const currentRunningSpec = this.currentRunningSpec; 362 const currentRunningSuite = this.currentRunningSuite; 363 if (currentRunningSpec) { 364 currentRunningSpec.fail = assertError; 365 } else { 366 currentRunningSuite.hookError = assertError; 367 } 368 throw assertError; 369 } 370 } catch (error: Error) { 371 const currentRunningSpec = this.currentRunningSpec; 372 const currentRunningSuite = this.currentRunningSuite; 373 if (currentRunningSpec) { 374 currentRunningSpec.fail = error; 375 } else { 376 currentRunningSuite.hookError = error; 377 } 378 } 379 }; 380 }; 381 const assertTrue = matchers.assertTrue; 382 if (assertTrue) { 383 this.assertTrue = booleanFn('assertTrue', assertTrue); 384 } 385 386 const assertFalse = matchers.assertFalse; 387 if (assertFalse) { 388 this.assertFalse = booleanFn('assertFalse', assertFalse); 389 } 390 } 391 392 addAnytypeAssert(matchers: AssertMatcherIF) { 393 const equalFn = (matcherName: string, assertFn: (actualValue: AnyType, expected: AnyType[]) => AssertResult) => { 394 return (arg: AnyType) => { 395 try { 396 const result = assertFn(this.actualValue, [arg]); 397 if (this.isNot) { 398 result.pass = !result.pass; 399 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg, result.message); 400 } 401 if (!result.pass) { 402 const assertError = new AssertException(result.message); 403 const currentRunningSpec = this.currentRunningSpec; 404 const currentRunningSuite = this.currentRunningSuite; 405 if (currentRunningSpec) { 406 currentRunningSpec.fail = assertError; 407 } else { 408 currentRunningSuite.hookError = assertError; 409 } 410 throw assertError; 411 } 412 } catch (error: Error) { 413 const currentRunningSpec = this.currentRunningSpec; 414 const currentRunningSuite = this.currentRunningSuite; 415 if (currentRunningSpec) { 416 currentRunningSpec.fail = error; 417 } else { 418 currentRunningSuite.hookError = error; 419 } 420 } 421 }; 422 }; 423 const assertDeepEquals = matchers.assertDeepEquals; 424 if (assertDeepEquals) { 425 this.assertDeepEquals = equalFn('assertDeepEquals', assertDeepEquals); 426 } 427 428 const assertContain = matchers.assertContain; 429 if (assertContain) { 430 this.assertContain = equalFn('assertContain', assertContain); 431 } 432 433 const assertEqual = matchers.assertEqual; 434 if (assertEqual) { 435 this.assertEqual = equalFn('assertEqual', assertEqual); 436 } 437 438 } 439 440 addInstanceOfAssert(matchers: AssertMatcherIF) { 441 const equalFn = (matcherName: string, assertFn: (actualValue: AnyType, expected: string[]) => AssertResult) => { 442 return (arg: string) => { 443 try { 444 const result = assertFn(this.actualValue, [arg]); 445 if (this.isNot) { 446 result.pass = !result.pass; 447 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg, result.message); 448 } 449 if (!result.pass) { 450 const assertError = new AssertException(result.message); 451 const currentRunningSpec = this.currentRunningSpec; 452 const currentRunningSuite = this.currentRunningSuite; 453 if (currentRunningSpec) { 454 currentRunningSpec.fail = assertError; 455 } else { 456 currentRunningSuite.hookError = assertError; 457 } 458 throw assertError; 459 } 460 } catch (error: Error) { 461 const currentRunningSpec = this.currentRunningSpec; 462 const currentRunningSuite = this.currentRunningSuite; 463 if (currentRunningSpec) { 464 currentRunningSpec.fail = error; 465 } else { 466 currentRunningSuite.hookError = error; 467 } 468 } 469 }; 470 }; 471 472 const assertInstanceOf = matchers.assertInstanceOf; 473 if (assertInstanceOf) { 474 this.assertInstanceOf = equalFn('assertInstanceOf', assertInstanceOf); 475 } 476 } 477 478 addUndefinedAssert(matchers: AssertMatcherIF) { 479 const undefinedFn = (matcherName: string, assertFn: (actualValue: AnyType) => AssertResult) => { 480 return () => { 481 try { 482 const result = assertFn(this.actualValue); 483 if (this.isNot) { 484 result.pass = !result.pass; 485 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, 'undefined', result.message); 486 } 487 if (!result.pass) { 488 const assertError = new AssertException(result.message); 489 const currentRunningSpec = this.currentRunningSpec; 490 const currentRunningSuite = this.currentRunningSuite; 491 if (currentRunningSpec) { 492 currentRunningSpec.fail = assertError; 493 } else { 494 currentRunningSuite.hookError = assertError; 495 } 496 throw assertError; 497 } 498 } catch (error: Error) { 499 const currentRunningSpec = this.currentRunningSpec; 500 const currentRunningSuite = this.currentRunningSuite; 501 if (currentRunningSpec) { 502 currentRunningSpec.fail = error; 503 } else { 504 currentRunningSuite.hookError = error; 505 } 506 } 507 }; 508 }; 509 const assertUndefined = matchers.assertUndefined; 510 if (assertUndefined) { 511 this.assertUndefined = undefinedFn('assertUndefined', assertUndefined); 512 } 513 const assertNull = matchers.assertNull; 514 if (assertNull) { 515 this.assertNull = undefinedFn('assertNull', assertNull); 516 } 517 } 518 519 addThrowAssert(matchers: AssertMatcherIF) { 520 const throwFn = (matcherName: string, assertFn: (actualValue: () => void, args: string[]) => AssertResult) => { 521 return (arg1: string, arg2?: string) => { 522 try { 523 let arr = [arg1]; 524 if (arg2 !== undefined) { 525 arr = [arg1, arg2]; 526 } 527 const result = assertFn(this.actualValue as () => void, arr); 528 if (this.isNot) { 529 result.pass = !result.pass; 530 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg1, result.message); 531 } 532 if (!result.pass) { 533 const assertError = new AssertException(result.message); 534 const currentRunningSpec = this.currentRunningSpec; 535 const currentRunningSuite = this.currentRunningSuite; 536 if (currentRunningSpec) { 537 currentRunningSpec.fail = assertError; 538 } else { 539 currentRunningSuite.hookError = assertError; 540 } 541 throw assertError; 542 } 543 } catch (error: Error) { 544 const currentRunningSpec = this.currentRunningSpec; 545 const currentRunningSuite = this.currentRunningSuite; 546 if (currentRunningSpec) { 547 currentRunningSpec.fail = error; 548 } else { 549 currentRunningSuite.hookError = error; 550 } 551 } 552 }; 553 }; 554 const assertThrowError = matchers.assertThrowError; 555 if (assertThrowError) { 556 this.assertThrowError = throwFn('assertThrowError', assertThrowError); 557 } 558 const assertThrow = matchers.assertThrow; 559 if (assertThrow) { 560 this.assertThrow = throwFn('assertThrow', assertThrow); 561 } 562 } 563 564 addPromiseAssert(matchers: AssertMatcherIF) { 565 const promiseFn = (matcherName: string, assertFn: (actualPromise: Promise<AnyType>) => Promise<AssertResult>) => { 566 return async (): Promise<void> => { 567 try { 568 const result: AssertResult = await assertFn(this.actualValue as Promise<AnyType>); 569 if (this.isNot) { 570 result.pass = !result.pass; 571 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, 'undefined', result.message); 572 } 573 if (!result.pass) { 574 const assertError = new AssertException(result.message); 575 const currentRunningSpec = this.currentRunningSpec; 576 const currentRunningSuite = this.currentRunningSuite; 577 if (currentRunningSpec) { 578 currentRunningSpec.fail = assertError; 579 } else { 580 currentRunningSuite.hookError = assertError; 581 } 582 throw assertError; 583 } 584 } catch (error: Error) { 585 const currentRunningSpec = this.currentRunningSpec; 586 const currentRunningSuite = this.currentRunningSuite; 587 if (currentRunningSpec) { 588 currentRunningSpec.fail = error; 589 } else { 590 currentRunningSuite.hookError = error; 591 } 592 } 593 }; 594 }; 595 const assertPromiseIsPending = matchers.assertPromiseIsPending; 596 if (assertPromiseIsPending) { 597 this.assertPromiseIsPending = promiseFn('assertPromiseIsPending', assertPromiseIsPending); 598 } 599 const assertPromiseIsRejected = matchers.assertPromiseIsRejected; 600 if (assertPromiseIsRejected) { 601 this.assertPromiseIsRejected = promiseFn('assertPromiseIsRejected', assertPromiseIsRejected); 602 } 603 const assertPromiseIsResolved = matchers.assertPromiseIsResolved; 604 if (assertPromiseIsResolved) { 605 this.assertPromiseIsResolved = promiseFn('assertPromiseIsResolved', assertPromiseIsResolved); 606 } 607 } 608 609 addPromiseWithAssert(matchers: AssertMatcherIF) { 610 const promiseFn = ( 611 matcherName: string, 612 assertFn: (actualPromise: Promise<AnyType>, expectedValue: AnyType[]) => Promise<AssertResult> 613 ) => { 614 return async (arg: AnyType): Promise<void> => { 615 try { 616 const result: AssertResult = await assertFn(this.actualValue as Promise<AnyType>, [arg]); 617 if (this.isNot) { 618 result.pass = !result.pass; 619 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg, result.message); 620 } 621 if (!result.pass) { 622 const assertError = new AssertException(result.message); 623 const currentRunningSpec = this.currentRunningSpec; 624 const currentRunningSuite = this.currentRunningSuite; 625 if (currentRunningSpec) { 626 currentRunningSpec.fail = assertError; 627 } else { 628 currentRunningSuite.hookError = assertError; 629 } 630 throw assertError; 631 } 632 } catch (error: Error) { 633 const currentRunningSpec = this.currentRunningSpec; 634 const currentRunningSuite = this.currentRunningSuite; 635 if (currentRunningSpec) { 636 currentRunningSpec.fail = error; 637 } else { 638 currentRunningSuite.hookError = error; 639 } 640 } 641 }; 642 }; 643 const assertPromiseIsResolvedWith = matchers.assertPromiseIsResolvedWith; 644 if (assertPromiseIsResolvedWith) { 645 this.assertPromiseIsResolvedWith = promiseFn('assertPromiseIsResolvedWith', assertPromiseIsResolvedWith); 646 } 647 const assertPromiseIsRejectedWith = matchers.assertPromiseIsRejectedWith; 648 if (assertPromiseIsRejectedWith) { 649 this.assertPromiseIsRejectedWith = promiseFn('assertPromiseIsRejectedWith', assertPromiseIsRejectedWith); 650 } 651 } 652 653 addPromiseWithError(matchers: AssertMatcherIF) { 654 const promiseFn = ( 655 matcherName: string, 656 assertFn: (actualPromise: Promise<AnyType>, expectedValue: string[]) => Promise<AssertResult> 657 ) => { 658 return async (arg: string, arg1?: string): Promise<void> => { 659 try { 660 let arr = [arg]; 661 if (arg1 !== undefined) { 662 arr = [arg, arg1]; 663 } 664 const result: AssertResult = await assertFn(this.actualValue as Promise<AnyType>, arr); 665 if (this.isNot) { 666 result.pass = !result.pass; 667 result.message = LogExpectError.getErrorMsg(matcherName, this.actualValue, arg, result.message); 668 } 669 if (!result.pass) { 670 const assertError = new AssertException(result.message); 671 const currentRunningSpec = this.currentRunningSpec; 672 const currentRunningSuite = this.currentRunningSuite; 673 if (currentRunningSpec) { 674 currentRunningSpec.fail = assertError; 675 } else { 676 currentRunningSuite.hookError = assertError; 677 } 678 throw assertError; 679 } 680 } catch (error: Error) { 681 const currentRunningSpec = this.currentRunningSpec; 682 const currentRunningSuite = this.currentRunningSuite; 683 if (currentRunningSpec) { 684 currentRunningSpec.fail = error; 685 } else { 686 currentRunningSuite.hookError = error; 687 } 688 } 689 }; 690 }; 691 692 const assertPromiseIsRejectedWithError = matchers.assertPromiseIsRejectedWithError; 693 if (assertPromiseIsRejectedWithError) { 694 this.assertPromiseIsRejectedWithError = promiseFn( 695 'assertPromiseIsRejectedWithError', 696 assertPromiseIsRejectedWithError 697 ); 698 } 699 } 700 701 addAssert(matchers: AssertMatcherIF) { 702 this.addCloseAssert(matchers); 703 this.addNumAssert(matchers); 704 this.addFailAssert(matchers); 705 this.addNumWithoutExpectAssert(matchers); 706 this.addBooleanAssert(matchers); 707 this.addAnytypeAssert(matchers); 708 this.addInstanceOfAssert(matchers) 709 this.addThrowAssert(matchers); 710 this.addUndefinedAssert(matchers); 711 this.addPromiseAssert(matchers); 712 this.addPromiseWithAssert(matchers); 713 this.addPromiseWithError(matchers); 714 } 715} 716export { ExpectService }; 717