( context: Deno.TestContext, actual: unknown, msgOrOpts?: string | SnapshotOptions<unknown>, )
| 550 | message?: string, |
| 551 | ): Promise<void>; |
| 552 | export async function assertSnapshot( |
| 553 | context: Deno.TestContext, |
| 554 | actual: unknown, |
| 555 | msgOrOpts?: string | SnapshotOptions<unknown>, |
| 556 | ) { |
| 557 | const options = getOptions(msgOrOpts); |
| 558 | const assertSnapshotContext = AssertSnapshotContext.fromOptions( |
| 559 | context, |
| 560 | options, |
| 561 | ); |
| 562 | const testName = getTestName(context, options); |
| 563 | const count = assertSnapshotContext.getCount(testName); |
| 564 | const name = `${testName} ${count}`; |
| 565 | const expectedSnapshot = await assertSnapshotContext.getSnapshot( |
| 566 | name, |
| 567 | options, |
| 568 | ); |
| 569 | |
| 570 | assertSnapshotContext.pushSnapshotToUpdateQueue(name); |
| 571 | const serializer = options.serializer ?? serialize; |
| 572 | const actualSnapshot = serializer(actual); |
| 573 | if (getIsUpdate(options)) { |
| 574 | await assertSnapshotContext.registerTeardown(); |
| 575 | if (!equal(actualSnapshot, expectedSnapshot)) { |
| 576 | assertSnapshotContext.updateSnapshot(name, actualSnapshot); |
| 577 | } |
| 578 | } else { |
| 579 | if ( |
| 580 | !assertSnapshotContext.hasSnapshot(name) || |
| 581 | typeof expectedSnapshot === "undefined" |
| 582 | ) { |
| 583 | throw new AssertionError( |
| 584 | getErrorMessage(`Missing snapshot: ${name}`, options), |
| 585 | ); |
| 586 | } |
| 587 | |
| 588 | if (equal(actualSnapshot, expectedSnapshot)) { |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | throw new AssertionError( |
| 593 | getSnapshotNotMatchMessage(actualSnapshot, expectedSnapshot, options), |
| 594 | ); |
| 595 | } |
| 596 | |
| 597 | function getTestName( |
| 598 | context: Deno.TestContext, |
| 599 | options?: SnapshotOptions, |
| 600 | ): string { |
| 601 | if (options && options.name) { |
| 602 | return options.name; |
| 603 | } else if (context.parent) { |
| 604 | return `${getTestName(context.parent)} > ${context.name}`; |
| 605 | } |
| 606 | return context.name; |
| 607 | } |
| 608 | } |
| 609 |
no test coverage detected