marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
845 stars 59 forks source link

mocking promises? #32

Closed xenoterracide closed 3 years ago

xenoterracide commented 4 years ago

so I was going to use a deep mock for this, but didn't find a way to do it, is there a better way? if not could there be a better way?

    const axios: MockProxy<AxiosInstance> = mock<AxiosInstance>();
    const response: MockProxy<AxiosResponse> = mock<AxiosResponse>();
    response.data = {
      result: user,
    };
    axios.get.mockResolvedValue(response);
xenoterracide commented 3 years ago

another case/problem

I have this on my class, it uses typeorm

  @OneToOne(() => StripeCustomerEntity, ({ account }) => account, { lazy: true })
  readonly stripeCustomer!: Promise<Optional<StripeCustomerEntity>>;
    const mockAccount = mock<AccountEntity>();
    mockAccount.stripeCustomer; // this returns a promise and does not contain the methods for mocking its return

this compiles but the promise doesn't work

    const mockAccount = mock<AccountEntity>({
      stripeCustomer: Promise.resolve(customer0),
    });

    const customer1 = requireNonNullish(await repo.save(mockAccount));
    TypeError: Method Promise.prototype.then called on incompatible receiver [object Object]
        at Proxy.then (<anonymous>)
marchaos commented 3 years ago

Have you tried

  const mockAccount = mock<AccountEntity>();
  mockAccount.stripeCustomer.mockResolvedValue(value)
xenoterracide commented 3 years ago

I found that abit after updating this, which worked. but mocking the property that is a promise doesn't.

marchaos commented 3 years ago

should work since we now ignore then, Reopen if this is still an issue.