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

Function constructorSpy

testing/mock.ts:720–754  ·  view source on GitHub ↗

Wraps a constructor with a Spy.

(
  constructor: new (...args: Args) => Self,
)

Source from the content-addressed store, hash-verified

718
719/** Wraps a constructor with a Spy. */
720function constructorSpy<
721 Self,
722 Args extends unknown[],
723>(
724 constructor: new (...args: Args) => Self,
725): ConstructorSpy<Self, Args> {
726 const original = constructor;
727 const calls: SpyCall<Self, Args, Self>[] = [];
728 // @ts-ignore TS2509: Can't know the type of `original` statically.
729 const spy = class extends original {
730 // deno-lint-ignore constructor-super
731 constructor(...args: Args) {
732 const call: SpyCall<Self, Args, Self> = { args };
733 try {
734 super(...args);
735 call.returned = this as unknown as Self;
736 } catch (error) {
737 call.error = error as Error;
738 calls.push(call);
739 throw error;
740 }
741 calls.push(call);
742 }
743 static readonly name = original.name;
744 static readonly original = original;
745 static readonly calls = calls;
746 static readonly restored = false;
747 static restore() {
748 throw new MockError(
749 "Cannot restore: constructor cannot be restored",
750 );
751 }
752 } as ConstructorSpy<Self, Args>;
753 return spy;
754}
755
756/**
757 * Utility for extracting the arguments type from a property

Callers 1

spyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected