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

Function assertSnapshot

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

Source from the content-addressed store, hash-verified

550 message?: string,
551): Promise<void>;
552export 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

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