( context: Deno.TestContext, actual: unknown, msgOrOpts?: string | SnapshotOptions<unknown>, )
| 604 | message?: string, |
| 605 | ): Promise<void>; |
| 606 | export async function assertSnapshot( |
| 607 | context: Deno.TestContext, |
| 608 | actual: unknown, |
| 609 | msgOrOpts?: string | SnapshotOptions<unknown>, |
| 610 | ) { |
| 611 | const options = getOptions(msgOrOpts); |
| 612 | const assertSnapshotContext = AssertSnapshotContext.fromOptions( |
| 613 | context, |
| 614 | options, |
| 615 | ); |
| 616 | const testName = getTestName(context, options); |
| 617 | const count = assertSnapshotContext.getCount(testName); |
| 618 | const name = `${testName} ${count}`; |
| 619 | const expectedSnapshot = await assertSnapshotContext.getSnapshot( |
| 620 | name, |
| 621 | options, |
| 622 | ); |
| 623 | |
| 624 | assertSnapshotContext.pushSnapshotToUpdateQueue(name); |
| 625 | const serializer = options.serializer ?? serialize; |
| 626 | const actualSnapshot = serializer(actual); |
| 627 | if (getIsUpdate(options)) { |
| 628 | await assertSnapshotContext.registerTeardown(); |
| 629 | if (!equal(actualSnapshot, expectedSnapshot)) { |
| 630 | assertSnapshotContext.updateSnapshot(name, actualSnapshot); |
| 631 | } |
| 632 | } else { |
| 633 | if ( |
| 634 | !assertSnapshotContext.hasSnapshot(name) || |
| 635 | typeof expectedSnapshot === "undefined" |
| 636 | ) { |
| 637 | throw new AssertionError( |
| 638 | getErrorMessage(`Missing snapshot: ${name}`, options), |
| 639 | ); |
| 640 | } |
| 641 | |
| 642 | if (equal(actualSnapshot, expectedSnapshot)) { |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | throw new AssertionError( |
| 647 | getSnapshotNotMatchMessage(actualSnapshot, expectedSnapshot, options), |
| 648 | ); |
| 649 | } |
| 650 | |
| 651 | function getTestName( |
| 652 | context: Deno.TestContext, |
| 653 | options?: SnapshotOptions, |
| 654 | ): string { |
| 655 | if (options && options.name) { |
| 656 | return options.name; |
| 657 | } else if (context.parent) { |
| 658 | return `${getTestName(context.parent)} > ${context.name}`; |
| 659 | } |
| 660 | return context.name; |
| 661 | } |
| 662 | } |
| 663 |
no test coverage detected