MCPcopy Create free account
hub / github.com/denoland/std / assertSnapshot

Function assertSnapshot

testing/snapshot.ts:606–662  ·  view source on GitHub ↗
(
  context: Deno.TestContext,
  actual: unknown,
  msgOrOpts?: string | SnapshotOptions<unknown>,
)

Source from the content-addressed store, hash-verified

604 message?: string,
605): Promise<void>;
606export 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

Callers 2

snapshot_test.tsFile · 0.90
testFailedAssertionFunction · 0.90

Calls 13

getOptionsFunction · 0.90
getIsUpdateFunction · 0.90
equalFunction · 0.90
getErrorMessageFunction · 0.90
getTestNameFunction · 0.85
fromOptionsMethod · 0.80
getCountMethod · 0.45
getSnapshotMethod · 0.45
registerTeardownMethod · 0.45
updateSnapshotMethod · 0.45

Tested by

no test coverage detected