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

Using calledWith where a parameter in an object is a callback #44

Open
kobelobster opened this issue Jul 9, 2020 · 1 comment
Open

Comments

@kobelobster
Copy link

kobelobster commented Jul 9, 2020

I have the following code in my application

public get<T>(
      url: string,
      params: AxiosRequestConfig
  ): Promise<AxiosResponse<T>> {
  return this.axiosInstance.get<T>(url, params);
}

where params is something like

{
  baseURL: 'example.com',
  headers: {
    'X-Custom-Header': '...',      
  },
  transformResponse: (data) => {
     return JSON.parse(...callbackfunction(data));
  }

When I mock this using the following code

mockAxiosInstance
    .get
    .calledWith(
        'any',
        objectContainsKey('baseURL') &&
        objectContainsValue('example.com') &
        objectContainsKey('headers') &&
        objectContainsValue({'X-Session-Key'
        objectContainsKey('transformResponse
    )
    .mockReturnValue(new Promise(() => {}));

This works without any problems, however, as you can see I also have a callback function as an option. How would I mock that?

Thank you :) Your library helped me so far.

@kobelobster kobelobster changed the title Using calledWith where a parameter is an object Using calledWith where a parameter in an object is a callback Jul 9, 2020
@maaasyn
Copy link

maaasyn commented Apr 19, 2022

Hey, for those who end up here by googling, I may have a solution for your issue:

import {Matcher } from "jest-mock-extended";
import { isEqual } from "lodash";


const objectWithTheSameFields = <T>(expectedValue: T) =>
  new Matcher<T | unknown>((actualValue) => {
    return isEqual(actualValue, expectedValue);
  }, "different fields");


// my.test.ts

prismaMock?.someThing?.findMany
      .calledWith(
        objectWithTheSameFields({
          where: {
            name: { contains: query },
          },
        })
      )
      .mockResolvedValue([{...}]);
  

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