Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private properties prevent casting #25

Closed
Sytten opened this issue Apr 3, 2020 · 1 comment
Closed

Private properties prevent casting #25

Sytten opened this issue Apr 3, 2020 · 1 comment

Comments

@Sytten
Copy link
Contributor

Sytten commented Apr 3, 2020

This is a complex problem. I am a mocking a class from the Prisma library that looks like

export declare class PrismaClient<T extends PrismaClientOptions = {}, U = keyof T extends 'log' ? T['log'] extends Array<LogLevel | LogDefinition> ? GetEvents<T['log']> : never : never> {
  private fetcher;
  private readonly dmmf;
  private connectionPromise?;
  private disconnectionPromise?;
  private readonly engineConfig;
  private readonly measurePerformance;
  private engine: Engine;
  private errorFormat: ErrorFormat;
  get event(): EventDelegate;
}

I store an instance of this class in a context type:

export type Context = {
  prisma: PrismaClient;
};

For my tests, I created a mock type for the context:

export type MockContext = {
  prisma: MockProxy<PrismaClient>;
};

When I want to pass this mock context to the function to test, it is unable to cast the type to a Context because of the private properties:

Conversion of type 'MockContext' to type 'Context' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Types of property 'prisma' are incompatible.
    Type 'MockProxy<PrismaClient<{}, never>>' is missing the following properties from type 'PrismaClient<{}, never>': fetcher, dmmf, engineConfig, measurePerformance, and 3 more.

So for now I did a hack where I store a casted context in my mocked context:

export type MockContext = {
  prisma: MockProxy<PrismaClient>;
  ctx: Context;
};

export const createMockContext = (): MockContext => {
  const baseCtx = {
    prisma: mockDeep<PrismaClient>(),
  };

  const mockCtx = {
    ...baseCtx,
    ctx: baseCtx as Context,
  };

  return mockCtx;
};

But it would be great if I could just cast the mock context directly.
Thanks a lot!

@marchaos
Copy link
Owner

This is fixed in 1.0.15. reopen this issue if you have any issues

Beraliv pushed a commit to Beraliv/jest-mock-extended that referenced this issue May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants